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'); });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!