如何通过ajax发送以下数组中的每个元素? [英] How do I send through ajax each element from the following array?

查看:157
本文介绍了如何通过ajax发送以下数组中的每个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过ajax发送以下数组中的每个元素.注意:每个请求必须 一旦上一个完成就可以制作. ['This','is','a','fake,'array']

Send through ajax each element from the following array. Note: Each request must be made once the previous has finished. [‘This’, ‘is’, ‘a’, ‘fake, ‘array’]

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

I am a little confused by this question because I thought Ajax is asynchronous, meaning the script keeps sending requests to the server without waiting for the reply.

推荐答案

我在谈论 JQuery Ajax .

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

beforeSend(本地事件)

beforeSend (Local Event)

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

This event, which is triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object (setting additional headers, if need be.)

错误(本地事件)

仅当请求发生错误时才调用此事件(您 不能同时有错误和带请求的成功回调.

This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request).

完成(本地事件)

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

This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.

成功(本地事件)

仅在请求成功(无错误)时调用此事件 来自服务器,数据没有错误.)

This event is only called if the request was successful (no errors from the server, no errors with the data).

更多文档.

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

Second, following your example (you have to complete this with your own data and this code is not tested, maybe it has some small sintax errors), an approximation is:

//  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();

这篇关于如何通过ajax发送以下数组中的每个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆