2015-02-24 29 views
0

我有兩個AJAX形式:Ajax.BeginForm總是excecuting一個「POST」

@using (Ajax.BeginForm("Index2","Home", 
    new AjaxOptions 
     { 
      UpdateTargetId = "result", 
      HttpMethod = "PUT" 
     }, 
    new 
     { 
      onclick = "Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));", 
      onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, httpMethod: 'PUT', updateTargetId: 'result' });" 
     })) 
{ 
    <input type="hidden" name="id" value='1'/> 
    <input type="submit" value="OK Put" /> 
} 

@using (Ajax.BeginForm("Index2","Home", 
    new AjaxOptions 
     { 
      UpdateTargetId = "result", 
      HttpMethod = "DELETE" 
     }, 
    new 
     { 
      onclick = "Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));", 
      onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, httpMethod: 'DELETE', updateTargetId: 'result' });" 
     })) 
{ 
    <input type="hidden" name="id" value='1'/> 
    <input type="submit" value="Error Delete" /> 
} 

在第一個,我excecuting一個PUT,第二個一DELETE,但在提琴手送花兒給人說,是POST

要繼續測試,我添加了一個代碼(這個代碼僅與測試的建議)

(function() { 
    var origOpen = XMLHttpRequest.prototype.open; 
    XMLHttpRequest.prototype.open = function (a) { 
     console.log("~>" + a); 
    //console.log(this); 
    var x = a; 
    this.addEventListener('load', function() { 
     //console.log(this); 
     if(x == "POST"){ 
       alert("Was a POST"); 
      } 
     }); 
     origOpen.apply(this, arguments); 
    }; 
})(); 

爲什麼我的其他HttpVerbs總是被excecuted爲POST

+0

@PatrickHofman基本上,這是不同的,因爲這個問題它是基於線程的解決方案,並與'MVC'符號相關型號,問題不是以'jquery'的,它與'Razor' – MrMins 2015-02-24 14:35:41

回答

2

看來從GET和POST是僅有的兩個由HttpMethod財產支持動詞docs

獲取或設置HTTP請求方法( 「GET」 或 「郵報」)。

那麼,什麼可能發生的是,PUT或DELETE使用的是不被接受,並且屬性默認爲POST:

HTTP請求方法。默認值是「Post」。

+0

就是你打我吧! :< – 2015-02-24 14:39:46

+0

我發現這個值:'XMLHttpRequest.prototype.open = function(a,b){console.log(b); }); '這就是返回:'〜>方法:POST - http:// localhost:5773/Home/Index2?Length = 4&id = 1&X-Requested-With = XMLHttpRequest&X-HTTP-Method-Override = DELETE'和'HTTP-Method -Override'有'PUT'和'DELETE'重寫方法 – MrMins 2015-02-24 14:45:27