Eri*_*cia 23 ajax asp.net-mvc jquery
我已经阅读了一段时间,发现你可以使用以下方法调用控制器动作:
$.ajax("MyController/MyAction", function(data) {
alert(data);
});
Run Code Online (Sandbox Code Playgroud)
这是否意味着我应该将MicrosoftMvcAjax.js或MicrosoftAjax.js与Jquery lib一起添加?
另外,$ .ajax()函数中第二个参数应该包含什么?
最后,stackoverflow中或站点外的任何其他链接可能对asp.net mvc w/ajax和jquery有帮助吗?
谢谢.
rob*_*nal 41
你可以从这里开始阅读jQuery.ajax()
实际上,Controller Action是一种可以通过Url访问的公共方法.因此,可以通过Ajax调用(MicrosoftMvcAjax或jQuery)调用Action.对我来说,jQuery是最简单的.它在我上面给出的链接中有很多例子.ajax调用的典型示例是这样的.
$.ajax({
// edit to add steve's suggestion.
//url: "/ControllerName/ActionName",
url: '<%= Url.Action("ActionName", "ControllerName") %>',
success: function(data) {
// your data could be a View or Json or what ever you returned in your action method
// parse your data here
alert(data);
}
});
Run Code Online (Sandbox Code Playgroud)
更多例子可以在这里找到
Gle*_*rie 30
以前的响应仅限ASP.NET
你需要引用jquery(可能来自CDN):http: //ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js
然后是一个类似的代码块,但更简单......
$.ajax({ url: '/Controller/Action/Id',
success: function(data) { alert(data); },
statusCode : {
404: function(content) { alert('cannot find resource'); },
500: function(content) { alert('internal server error'); }
},
error: function(req, status, errorObj) {
// handle status === "timeout"
// handle other errors
}
});
Run Code Online (Sandbox Code Playgroud)
我已经添加了一些必要的处理程序,如果你正在调试代码,404和500会一直发生.此外,许多其他错误(如超时)将通过错误处理程序过滤掉.
ASP.NET MVC控制器处理请求,因此您只需要请求正确的URL,控制器就会接收它.此代码示例在ASP.NET以外的环境中工作
| 归档时间: |
|
| 查看次数: |
173590 次 |
| 最近记录: |