繁体   English   中英

如何通过ajax发送以下数组中的每个元素?

[英]How do I send through ajax each element from the following array?

通过ajax发送以下数组中的每个元素。 注意:每个请求都必须在前一个请求完成后进行。 ['This','is','a','fake,'array']

这个问题让我有些困惑,因为我认为Ajax是异步的,这意味着脚本会一直向服务器发送请求,而无需等待答复。

***被否决了,所以要澄清一下:它在问题说明中特别指出必须同步进行REQUEST。 我确实意识到,有更好的方法可以通过def / promises异步执行此操作,因此结果的顺序保持不变,但这不是请求。

Ajax有一个异步参数,您可以将其设置为false,它将阻塞直到调用完成。

每个文档:

async(默认:true)类型:Boolean默认情况下,所有请求都是异步发送的(即默认情况下设置为true)。 如果需要同步请求,请将此选项设置为false。 跨域请求和dataType:“ jsonp”请求不支持同步操作。 请注意,同步请求可能会暂时锁定浏览器,从而在请求处于活动状态时禁用任何操作。 从jQuery 1.8开始,不建议使用async:false和jqXHR($ .Deferred); 您必须使用成功/错误/完成回调选项,而不要使用jqXHR对象的相应方法,例如jqXHR.done()。

http://api.jquery.com/jquery.ajax/

例:

$.each(["This", "is", "a", "fake", "array"], function( index, value ) {
 $.ajax({
  type: 'POST',
  dataType: 'json',
  url: '/echo/json/',
  data : { json: JSON.stringify( value ) },
  async: false,
  success: function(data) { alert(data);}  
 }); 
});

工作提琴手示例: https : //jsfiddle.net/zm9bb4xk/

我在谈论JQuery Ajax

因此,首先,基于文档,Ajax具有许多在特定时间运行的事件,例如:

beforeSend(本地事件)

此事件在启动Ajax请求之前触发,允许您修改XMLHttpRequest对象(如果需要,可以设置其他头)。

错误(本地事件)

仅在请求发生错误时才调用此事件(对于请求,您永远不能同时发生错误和成功回调)。

完成(本地活动)

无论请求是否成功,都将调用此事件。 即使对于同步请求,您将始终收到完整的回调。

成功(本地活动)

仅当请求成功时才调用此事件(服务器无错误,数据无错误)。

更多文档。

其次,按照您的示例 (您必须使用自己的数据来完成此操作,并且此代码未经测试,可能有一些小的sintax错误), 其近似值为:

//  Variables
var myArray = ["This", "is", "a", "fake", "array"];
var globalIndex = 0;


//  Function for Ajax Calls
function myFunction(){
    $.ajax({
        url: 'myURL',                       //  Your own controller/url

        type: "GET",                        //  Or POST

        dataType: "json",                   //  Or other datatype

        data: {
            arrayContent: myArray[globalIndex]  //  arrayContent = your controller param name   
        },      

        /**
         * A function to be called if the request succeeds.
         */
        success: function(data) {       

            alert('Load was performed. Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information! '); 
            alert(data);                //  Do what you want with your data, this is an example 

            globalIndex = globalIndex +1;           
            //  Recursive/next call if current call is finished OK and there are elements
            if(globalIndex < myArray.length){
                myFunction();
            }
        },

        /**
         * A function to be called if the request fails. 
         */
        error: function(jqXHR, textStatus, errorThrown) {

            alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');

            alert('<p>status code: '+jqXHR.status+'</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
            console.log('jqXHR:');
            console.log(jqXHR);
            console.log('textStatus:');
            console.log(textStatus);
            console.log('errorThrown:');
            console.log(errorThrown);

            //  We don't do a recursive/next call because current call has failed
        },
    });
}

//  First call to myFunction

myFunction();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM