Ajax.BeginForm 总是在执行 'POST'
Ajax.BeginForm is always excecuting a 'POST'
我有两个ajax表格:
@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" />
}
在第一个中,我执行一个 PUT
,第二个执行一个 DELETE
,但在 fiddler 中总是说是 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 总是被执行为 POST
?
从docs看来,GET和POST只是HttpMethod
支持的两个动词 属性:
Gets or sets the HTTP request method ("Get" or "Post").
所以可能发生的是您正在使用的 PUT 或 DELETE 不被接受,并且 属性 默认为 POST:
The HTTP request method. The default value is "Post".
一种解决方法是 "override" Ajax.BeginForm 的 OnBegin
HTTP 方法:
首先设置JavaScript函数在OnBegin
调用
@using (Ajax.BeginForm("", "", null, new AjaxOptions()
{
OnBegin = "beginSubmit",
...
然后 - 在 JavaScript 函数中 - 将请求类型更改为 PUT
:
<script type="text/javascript">
function beginSubmit(xhr, o) {
o.type = "PUT";
}
</script>
我有两个ajax表格:
@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" />
}
在第一个中,我执行一个 PUT
,第二个执行一个 DELETE
,但在 fiddler 中总是说是 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 总是被执行为 POST
?
从docs看来,GET和POST只是HttpMethod
支持的两个动词 属性:
Gets or sets the HTTP request method ("Get" or "Post").
所以可能发生的是您正在使用的 PUT 或 DELETE 不被接受,并且 属性 默认为 POST:
The HTTP request method. The default value is "Post".
一种解决方法是 "override" Ajax.BeginForm 的 OnBegin
HTTP 方法:
首先设置JavaScript函数在OnBegin
@using (Ajax.BeginForm("", "", null, new AjaxOptions()
{
OnBegin = "beginSubmit",
...
然后 - 在 JavaScript 函数中 - 将请求类型更改为 PUT
:
<script type="text/javascript">
function beginSubmit(xhr, o) {
o.type = "PUT";
}
</script>