发布使用jQuery AJAX来ASP.NET MVC 1.0 [英] Post with jQuery AJAX to ASP.NET MVC 1.0
问题描述
我在做一个jQuery AJAX的帖子是这样的:
变种形式= $(#formid);
form.submit(函数()
{
$阿贾克斯(
{
键入:POST,
网址:form.attr(行动),//指向NewClient
数据:form.serialize(),
成功:函数(MSG){警报(OK+味精); },
错误:函数(REQ,状态,ERR){警报(ERR ='+犯错+状态='+状态); }
});
返回false;
});
在ASP.MVC一边,我有这样的事情:
的[AcceptVerbs(HttpVerbs.Post)
公共虚拟的ActionResult NewClient()
{
//只是一个测试...
返回null;
}
什么是对NewClient功能的jQuery正确的结果类型工作的权利?正如现在它工作在FF和铬,但未能在IE8(在IE7作品) - 即在IE8中,我得到了错误警报(修改的:错误是因为一个调试器;在JS调用呼叫)。在任何情况下,有什么推荐的方式:
- 通过jQuery传递一个表单,ASP.NET MVC(的$()。序列化()似乎做工精细,这是推荐的方式?)
- 从服务器端返回成功或失败,如果失败的消息某种我可以显示给客户端。我如何告诉jQuery的,该方法/调用失败或不?
感谢。
编辑:
的HTML看起来是这样的:
<形式ID =formid行动=/客户/ newclient>
...输入字段...
< /形式GT;
form.serialize()是张贴在jQuery的形式正确的方式。
在错误处理方面,你有它的要点。因为如果服务器返回一个失败的HTTP code失败jQuery将只考虑请求。如果您要确定一个逻辑错误,即你的应用程序以某种方式失败,并希望返回一个HTML / JSON响应,指示出了什么问题(这将有一个有效的HTTP code),那么你就需要添加一个错误标志您的回复,并检查它的成功的处理程序。
我一直很喜欢本·纳德尔的方法给它。
我也<一href="http://stackoverflow.com/questions/1264419/jquery-ajax-responses-in-asp-net-mvc/1264449#1264449">answered以pretty的详细code例如的前一个类似的问题。
在重组所有的jQuery code,虽然,我想看看发生了什么事情在IE8与像小提琴手的工具。
I'm doing a jQuery AJAX post like this:
var form = $("#formid");
form.submit(function()
{
$.ajax(
{
type: "POST",
url: form.attr("action"), // points to NewClient
data: form.serialize(),
success: function(msg) { alert('ok ' + msg); },
error: function(req, status, err) { alert('err=' + err + ' status=' + status); }
});
return false;
});
On the ASP.MVC side I have something like this:
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult NewClient()
{
// just a test...
return null;
}
What's the right result type for the NewClient function for jQuery to work right? As is right now it works in FF and Chrome, but fails in IE8 (works in IE7) -- i.e. in IE8 I get the error alert (edit: the error was because of a "debugger;" call in the JS call). In any case, what's the recommended way to:
- Pass a form to ASP.NET MVC via jQuery (the $().serialize() seems to work fine, is this the recommended way?)
- From the server side to return either Success or Failure, and if it fails some kind of message I could display to the client. How do I tell jQuery that the method / call failed or not?
Thanks.
EDIT:
The html looks like this:
<form id="formid" action="/client/newclient">
... input fields ...
</form>
form.serialize() is the correct way to post a form in jQuery.
In terms of error handling, you've got the gist of it. jQuery will only consider the request as failed if the server returns a failure HTTP code. If you want to determine a "logical failure", i.e. your application failed somehow, and wants to return an html/json response indicating what went wrong (which would have a valid HTTP code), then you need to add an error flag to your response, and check for it in the "success" handler.
I always liked Ben Nadel's approach to it.
I've also answered a similar question before with a pretty detailed code example.
Before restructuring all your jQuery code though, I would look at what's going on in IE8 with a tool like Fiddler.
这篇关于发布使用jQuery AJAX来ASP.NET MVC 1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!