将变量传递给jQuery ajax成功或错误函数

Nik*_*ikG 9 ajax jquery json

我使用JQuery $ .ajax循环一组JSON URL并将结果下载到数组中.一些URL返回404 - 我正在处理并显示为div消息.

但是,我似乎无法传递URL,更确切地说,它总是只传递数组中的最后一个URL.

我假设这是因为ajax是异步的并且需要更长的时间来完成,但我不确定如何确保只有当前的JSON url(或变量)显示在SUCCESS或ERROR上

我的代码:

 // For every URL loop through 'baseitems' array
 for (var i = 0; i < baseitems.length; i++) {

  // This is where I'm hoping to store the current URL as a variable to pass to the end-user on Success or Error
  var caturl = baseURL + "/" + baseitems[i];

  // Get the data via JSON
  $.ajax({
    type: "GET",
    url: caturl,
    dataType: "json",
    async: true, // set so that the variables caturl get updated below
    success: function (result) {
        // Success: store the object into catalog array
        cat.unshift(result);
        $('#showdata').prepend("Loaded: " + caturl + "</br>");  // still buggy here - probably async JSON issue
    },
    error: function (xhr, textStatus, error) {
        // Error: write out error
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
        $('#showdata').prepend("ERROR : '" + error + "' trying to access: " + caturl + "</br>");  // still buggy here  - probably async JSON issue
    }
});
Run Code Online (Sandbox Code Playgroud)

}

**更新:工作代码**

使用@charlietfl帮助完成的工作代码+一些好的东西,如成功/错误代码+加载的URL数量如下.谢谢charlietfl&peacemaker!

                $.ajax({
                    type: "GET",
                    url: caturl,
                    dataType: "json",
                    async: true, // set so that the variables caturl get updated below
                    beforeSend: function (jqXHR, settings) {
                        /* add url property and get value from settings (or from caturl)*/
                        jqXHR.url = settings.url;
                    },
                    success: function (result, textStatus, jqXHR) {
                        // Success: store the object into catalog array
                        var url = jqXHR.url;
                        cat.unshift(result);
                        $('#showdata').prepend("<font size=\"1\">Loading: " + url + " status: " + textStatus + "</font></br>");
                        successcount += 1;

                    },
                    /* error to be deprecated in jQuery 1.8 , superseded by "fail" */
                    error: function (jqXHR, textStatus, error) {
                        var url = jqXHR.url;
                        /* replace caturl with url in your append */
                        $('#showdata').prepend("<font size=\"1\" color=\"red\">ERROR : '" + error + "' trying to access: " + url + "</font></br>");
                    },
                    complete: function (jqXHR, textStatus) {
                        $('#showdata').prepend("<font size=\"3\">Loaded <b>" + successcount + "</b> of " + baseitems.length + " total catalogs.</font></br>")
                    }
                });
Run Code Online (Sandbox Code Playgroud)

cha*_*tfl 13

这是一种方式.该beforeSend回调选项,您可以访问两个jqXHR对象和AJAX设置对象.

您将无法caturl在附加错误中使用,因为它不会与请求抛出错误同步.

  $.ajax({
    /* url, data ...& other opts*/
    beforeSend:function( jqXHR, settings){
        /* add url property and get value from settings (or from caturl)*/
         jqXHR.url= settings.url;
   },
   /* error to be deprecated in jQuery 1.8 , superseded by "fail" */
   error: function(jqXHR, , textStatus, error){
       var url=jqXHR.url;
     /* replace caturl with url in your append */ 
     $('#showdata').prepend("ERROR : '" + error + "' trying to access: " + url + "</br>");
   }
Run Code Online (Sandbox Code Playgroud)

编辑:基于第三方API评论,重要的是要从$ .ajax API中直接识别以下内容

从远程服务器检索数据时(只能使用脚本或jsonp数据类型),永远不会触发错误回调和全局事件.