2015-02-24 57 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