ASP.NET MVC3 & ASP.NET MVC3 jquery.getJSON 未发出请求
我正在 MVC 3 站点上工作,一切正常,直到我使用 nuget 升级到 jquery1.7.1。现在我的 $.getJSON 调用没有向服务器发出请求。 Fiddler 不会显示在执行 $.getJSON 之前和之后向服务器发出的请求和代码。任何想法将不胜感激。
这是一个示例客户端脚本:
var srvBaseUrl = "http://localhost:15627/Home/";
$.getJSON(srvBaseUrl + "GetInfo", { 'id': 1 }, function (allData) {
alert(allData);
});
以及相应的服务器代码:
[HttpGet]
public ActionResult GetInfo(int id)
{
return Json(id, JsonRequestBehavior.AllowGet);
}
我测试了服务器代码是否可以在 Fiddler 中构建请求。
I am working on a MVC 3 site and everything was working until I util I upgraded to jquery1.7.1 using nuget. Now my $.getJSON calls are not making the request to the server. Fiddler does not show a request being made back to the server and code before and after the $.getJSON is executing. Any ideas would be appreciated.
Here is a sample client script:
var srvBaseUrl = "http://localhost:15627/Home/";
$.getJSON(srvBaseUrl + "GetInfo", { 'id': 1 }, function (allData) {
alert(allData);
});
and the corresponding server code:
[HttpGet]
public ActionResult GetInfo(int id)
{
return Json(id, JsonRequestBehavior.AllowGet);
}
I tested that the server code works building up the request in Fiddler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到了类似的问题,原来是JQuery 缓存了结果。这可以解释您上面的评论,即它在您进行更改后开始起作用。
如果您查看 http://api.jquery.com/jQuery.getJSON/ ,您将看到
$.getJSON
只是简写如果您使用
$.ajax
而不是简写$.getJSON
您可以禁用使用cache
参数进行缓存,意思是您应该能够将代码替换为:I had a similar problem, it turned out that JQuery was caching the results. This would explain your comments above that it started working after you made a change.
If you have a look at http://api.jquery.com/jQuery.getJSON/, you will see that
$.getJSON
is just shorthand forIf you use the
$.ajax
instead of the shorthand$.getJSON
you can disable caching using thecache
parameter, meaning you should be able to replace your code with:Nuget 不会使用对已安装的最新版本 jQuery 的更新引用来更新您的
Views/Shared/_Layout.cshtml
文件。您需要自己更新它:编辑
假设这一切都按照您所说的顺序进行,请确保您的 getJSON 调用位于文档就绪函数内,因为它对我来说按原样工作。不过,我会更改您构建 Url 的方式以使用辅助方法:
这在使用控制器操作的库存 ASP.NET MVC3 Internet 应用程序模板项目中测试得很好。
Nuget is not going to update your
Views/Shared/_Layout.cshtml
file with the updated reference to the latest version of jQuery installed. You will need to update this yourself:EDIT
Assuming that is all in order like you said, make sure your getJSON call is inside a document ready function as it work as-is for me. I would however change how you are building the Url to use the helper method instead:
This tested out fine in a stock ASP.NET MVC3 Internet Application template project using your controller action as-is.