JQuery Ajax 调用通常不适用于 Safari 6
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18778814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Ajax call often not working on Safari 6
提问by Cataclysm
My Ajax call is really simple as below:
我的 Ajax 调用非常简单,如下所示:
function ajax(reqUrl, params , callback) {
console.log("Request URL "+reqUrl);
var cond;
cond = $.ajax({
type: 'post',
url: reqUrl,
data: params,
error:function(){ alert("some error occurred") },
success: callback
});
console.log("Server response "+cond.readyState);
}
// Call it as
var url = "/getResult";
var params = {};
params.param1 = "test1";
params.param2 = "test2";
ajax(url, params, function(returnCallback) {
console.log(returnCallback);
alert("Success");
});
That works fine in most cases. But sometimes (about 1 times in 3) it doesn't return anything to callback.
在大多数情况下,这很好用。但有时(大约 3 次中的 1 次)它不会返回任何回调。
I found many questions and answers for Not working ajax in Safari but fine in chrome and FireFox. My problem is different from them, because it's fine most of the time(I don't mean it was not fine usually because when I refresh my browser, that may cause my ajax call to work).
我发现了很多关于Not working ajax in Safari but good in chrome 和 FireFox 的问题和答案。我的问题与他们不同,因为大多数时候它都很好(我并不是说它通常不好,因为当我刷新浏览器时,这可能会导致我的 ajax 调用起作用)。
My main question is why does my ajax call sometimes fail? I don't get any errors on my JS console. When this situation, I refresh my browser to get my ajax call to. Any Ideas?
我的主要问题是为什么我的 ajax 调用有时会失败?我的 JS 控制台上没有任何错误。在这种情况下,我刷新浏览器以获取 ajax 调用。有任何想法吗?
Update:
更新:
I found that sometimes my ajax call method didn't call out because console.log("Request URL "+reqUrl);did not execute. When I don't want to refresh my browser, I clicked many times on my page's link to produce result. will something late to execute?
我发现有时候我的ajax调用方法没有调用出来是因为console.log("Request URL"+reqUrl); 没有执行。当我不想刷新浏览器时,我多次单击页面链接以产生结果。会晚点执行吗?
回答by Cataclysm
Finally, I found error .. Safari doesn't reload my JavaScript files again even disable Cache. So I put all of my JS code into:
最后,我发现错误.. Safari 不会再次重新加载我的 JavaScript 文件,甚至禁用缓存。所以我把我所有的 JS 代码放入:
$(document).ready(function(){
// start load my js functions
init();
});
to reload my JS files when my page was ready. Cheer !
当我的页面准备好时重新加载我的 JS 文件。加油!
回答by markin
I also met this problem.
我也遇到了这个问题。
When I moved all code into $(function() {})
, it worked.
当我将所有代码移入 时$(function() {})
,它起作用了。
After that, I found I had defined a variable named key
, which caused the problem.
之后,我发现我定义了一个名为 的变量key
,这导致了问题。
Just rename it, all things will be running.
只需重命名它,一切都会运行。
回答by Muhammad Hussain wazir
This is use for MVC application.
这是用于 MVC 应用程序。
$.ajax({
type: "POST",
url:'getResult',
data: "{userName :'" + userName + "',password :'" + password + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("here" + data.toString());
});
For Asp.net Application :
对于 Asp.net 应用程序:
$.ajax({
type: "POST",
url:'getResult',
data: "{userName :'" + userName + "',password :'" + password + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("here" + data.toString());
});
if u have still the issue than please post ur complete code here. i will test and reply soon
如果您仍有问题,请在此处发布您的完整代码。我会尽快测试并回复
回答by Linkmichiel
This seems to be a Safari issue. In this postthere is a suggestion to add a beforeSend to your ajax-request.
这似乎是 Safari 的问题。在这篇文章中,建议在您的 ajax 请求中添加 beforeSend。
In your case:
在你的情况下:
cond = $.ajax({
type: 'post',
url: reqUrl,
data: params,
beforeSend: function (event, files, index, xhr, handler, callBack) {
$.ajax({
async: false,
url: 'closeconnection.php' // add path
});
},
error:function(){ alert("some error occurred") },
success: callback
});
回答by Muhammad Hussain wazir
Please Test below Code. it is working fine.
请测试下面的代码。它工作正常。
$.ajax({
type: "POST",
url:'@Url.Action("getResult","Controller")',
data: "{userName :'" + userName + "',password :'" + password + "' }",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.toString());
});