douna1941 2013-09-16 22:48
浏览 62
已采纳

调用本地更新脚本的JQuery AJAX在Safari中失败,适用于Chrome和FF

Making an AJAX call on a local script to update dbase using POST. Error reporting is not giving me much to go on. Works in IE, Chrome and FF, but not Safari 6. Other JQuery features are working in Safari, and Firebug DOM tab is showing JQuery has. Here is the AJAX code block.

$.ajax({
    type: "POST",
    url: "updatead3.php",
    data: {pos_data: dd_elm},   //index array created earlier
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
}).done (function(data) {
    if(console && console.log) {
       console.log("AJAX success: " + data);
    }
}).fail(function(obj,status,error) {
     alert("AJAX Error:" + obj.error);
});

alert("Changes were saved!");
location.reload();
)};
)}:

Under the Firebug Scripts tab > Inspect jquery-1.9.1.js menu option, "Access to restricted URI denied" is being displayed. But if this were a cross domain issue, I wouldn't expect any JQuery functionality. Thanks in Advance

  • 写回答

1条回答 默认 最新

  • doujingao6210 2013-09-16 23:28
    关注

    When dealing with $.ajax (or any asynchronous code for that matter), one should always remember: 'below' doesn't mean 'after'.

    To make some code executed right after $.ajax() call is resolved, successfully or not, it's not enough just to write it under $.ajax - actually, unless it introduces a waiting block (like alert does), its code will always be executed before any ajax callback has a chance to work.

    Instead you should use methods provided by the Promise interface (and jqXHR object, returned by $.ajax):

    • .done(), which is fired when AJAX call is resolved successfully
    • .fail(), which is fired when AJAX call is resolved with an error
    • .always(), which is fired when AJAX call is resolved anyway (and all the callbacks specified in .done/.fail methods have been completed too).

    In this particular case you trigger the page's reload while the AJAX call is still processed. That's confusing for some browsers, first, and actually in most cases prevents you from correctly processing the results of AJAX.

    What should be done instead is placing the code into $.ajax().always() callback:

    $.ajax({ some: 'settings'})
     .done(function() { console.log('SUCCESS :)'); })
     .fail(function() { console.log('FAIL :('); })
     .always(function() { console.log('Doh, I\'m fired anyway'); });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度