jQuery的Ajax的工作原理返回MVC 3部分观点,但jQuery的负载不 [英] jQuery AJAX works to return mvc 3 partial view but jQuery load doesn't
问题描述
我试图填充一个div与MVC 3的局部视图我的控制器看起来是这样的:
I'm trying to populate a div with a partial view in MVC 3. My controller looks like this:
[HttpPost]
public ActionResult GetCustomerList(string searchString)
{
var custService = new CustomerViewModels();
var custListVM = custService.GetSearchList(searchString);
return PartialView("GetCustomersList", custListVM);
}
和我的jQuery的调用如下所示:
and my jQuery call looks like this:
function getCustomerList(searchCriteria) {
$.ajax({
url: 'Home/GetCustomerList',
type: 'POST',
async: false,
data: { searchString: searchCriteria },
success: function (result) {
$("#customerTabBody").html(result);
}
});
};
它工作正常。我开始怀疑我是否可以使用jQuery的load方法做同样的事情少了很多code,写这样的:
It works fine. I began to wonder if I could use the jQuery load method to do the same thing with a lot less code and wrote this:
function getCustomerList(searchCriteria) {
$("#customerTabBody").load('Home/GetCustomerList', { searchString: searchCriteria });
};
它返回无法找到一个错误,说资源。我使用@ Url.Action尝试,但它连接codeS同一个控制器的路径我已经很难codeD。在萤火虫我可以看到正被发布的URL是相同的并且searchString参数在这两种情况下格式相同。
It returns an error saying the resource can't be found. I've tried using @Url.Action but it encodes the same controller path I have hard coded. In Firebug I can see that the URL that is being posted to is the same and the searchString parameter is identically formatted in both instances.
有什么区别 - 为什么负载无法正常工作
What's the difference - why does load not work?
感谢
推荐答案
jQuery的文档来负荷说: ......
Jquery docs for load says...
The POST method is used if data is provided as an object; otherwise, GET is assumed.
因为你逝去的数据作为一个对象,它根据文档是一个POST调用。此外,您也越来越需要从控制器的数据通过$就这么控制器的操作方法似乎是正常的。发送使用负载Ajax请求的时候可能会有一些错误。你可以在Firebug检查此。
since you are passing data as an object, it has to be a post call according to docs. Furthermore, you are getting required data from controller through $.ajax so controller action method seems to be alright. There may be some error when sending the ajax request using load. you can inspect this in firebug.
这篇关于jQuery的Ajax的工作原理返回MVC 3部分观点,但jQuery的负载不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!