jQuery Ajax仅执行一次,但似乎每次都应调用 [英] jQuery Ajax only executing once, but appears to call every time it should

查看:120
本文介绍了jQuery Ajax仅执行一次,但似乎每次都应调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我已经设置了一个ajax调用,即在更改特定字段时应执行一个php脚本.该脚本首先在页面加载时执行.

Currently, I have set up an ajax call, that on change of a particular field it should execute a php script. The script first executes on page load.

AJAX呼叫

$j.ajax({url: prod_json_url, dataType: 'json', cache: true,
  beforeSend: function( xhr ) {
    xhr.overrideMimeType( 'text/plain; charset=x-user-defined' );
  },
  success: function(data) { 
    //things to do on success with JSON
  }
});

一切都可以在我的虚拟环境中很好地进行测试,但是当我将其部署到其他服务器时,会发生以下情况.

Everything works great in my virtual environment for testing, but when I deploy this to a different server the following happens.

1)在页面加载时,我可以查看正在执行的FireBug中的URL,并返回正确的JSON响应.记录表明它确实转到了要执行的脚本并返回了正确的数据.一切都按预期进行.

1) On page load, I can view the URL in FireBug being executed and returning the correct JSON Response. Logging shows that it did go to the script to be executed and returns the correct data. Everything acts as it should.

2)然后单击以更新该字段.通过再次在控制台中查看Firebug,正在执行正确的URL,但是,JSON响应不正确.它保持与页面加载相同的状态.当我添加日志记录时,似乎它实际上从未到达更新的URL. (与更新的参数与页面加载的URL相同).

2) I then click to update the field. By viewing Firebug in the Console again, the correct URL is being executed, however, the JSON response is incorrect. It keeps the same one that occurred on page load. When I added logging, it appears that it never actually reaches the updated URL. (Which is the same URL as page load with updated arguments).

3)如果我等待一段时间,然后重试,有时它的行为会再次恢复原样,但仅适用于一次执行.

3) If I wait some time, and try again, it sometimes behaves as it should again, but only for the one execution.

这种行为使我相信这是一个缓存问题.有谁知道如何解决这个问题或应该寻找或检查哪个变量?该数据库是完全相同的,我不确定还有什么可能导致这种情况.任何帮助是极大的赞赏!请让我知道更多信息是否也会有所帮助.

This behavior makes me believe it is a cacheing issue. Does anyone have an idea of how I can resolve this or what variable I should be looking for or checking? The database is exactly the same and I'm not sure what else might be causing this. Any help is greatly appreciated! Please let me know if more information would be helpful as well.

推荐答案

尝试将当前时间添加到要加载数据的url中:

Try adding the current time to the url you're loading data :

prod_json_url + '?ts=' + $.now()'

在IE中的Ajax请求上,缓存控制HTML标头显然被忽略,因此cache:false还不够.不过应该可以.

Cache control HTML headers apparently are ignore on Ajax requests in IE, therefore cache:false isn't enought. This should work though.

$.now()new Date().getTime()的快捷方式;

这篇关于jQuery Ajax仅执行一次,但似乎每次都应调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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