jQuery的$ .getJSON仅一次为每个控制。不会再次到达服务器 [英] jQuery $.getJSON works only once for each control. Doesn't reach the server again

查看:172
本文介绍了jQuery的$ .getJSON仅一次为每个控制。不会再次到达服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我的code

$.getJSON("./posts/vote/" + postId + "/1", null, function(result) {
   if (result.result == true)
      $("#pst" + postId + " > .pstside > .rank > .score").html(result.voteCount);
 });

我有一堆用code每个按钮的其中汇集了来自一个ASP.Net MVC控制器动作有些投票结果。

I have a bunch of buttons each with code which brings some voting results from an ASP.Net MVC controller action.

这行之有效的第一次单击该按钮,但如果再次单击什么也不会发生。没有到达服务器。

This works well the first time the button is clicked, but if it is clicked again nothing happens. Nothing reaches the server.

我测试的Firefox浏览器。

I am testing on FireFox.

这是怎么回事了?一些很怪的缓存?因为请求不会到达我的控制器中的第二次,JavaScript的似乎要执行好,但保持返回旧值。

What is going wrong? Some kind of weird caching? Because the request never reaches my controller the second time, the javascript seems to execute okay but keeps returning the old values.

推荐答案

听起来像一个浏览器的缓存问题(如果是,我pretty确保与IE发生),你可能需要使用的 $。阿贾克斯和缓存选项设置为false,因为它仅适用于dataType的剧本和 JSONP

Sounds like a browser cache issue (if is that I'm pretty sure that is happening with IE), you may want to use $.ajax and set the cache option to false, since it is false by default only for dataType script and jsonp:

$.ajax({
  type: "GET",
  url: "./posts/vote/" + postId + "/1",
  success: function (result) {
    if (result.result == true)
      $("#pst" + postId + " > .pstside > .rank > .score").html(result.voteCount);
  },
  dataType: "json",
  cache: false
});

或者你可以在全球范围,设置选项,所有的jQuery的Ajax功能使用前使用 $ .ajaxSetup $ .getJSON:

Or you could set that option globally, for all the jQuery Ajax functions, using $.ajaxSetup before using $.getJSON:

$.ajaxSetup({ cache: false });

编辑::您可以做一个POST请求返回JSON是这样的:

You can do a POST request returning JSON like this:

$.post("./posts/vote/" + postId + "/1", 
  function (result) {
    if (result.result == true)
      $("#pst" + postId + " > .pstside > .rank > .score").html(result.voteCount);
  }, "json");

如果你打算做了很多的 postJSON 的要求,你可以让你自己的功能:

If you plan to do a lot of postJSON request, you can make your own function:

jQuery.postJSON = function(url, data, callback) {
    jQuery.post(url, data, callback, "json") ;
};

和您就可以使用它,就像$ .getJSON

And you'll be able to use it just like $.getJSON

这篇关于jQuery的$ .getJSON仅一次为每个控制。不会再次到达服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆