Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Ajax一次只按顺序发布一个数组的1个值,并在继续之前等待响应?_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 如何使用Ajax一次只按顺序发布一个数组的1个值,并在继续之前等待响应?

Javascript 如何使用Ajax一次只按顺序发布一个数组的1个值,并在继续之前等待响应?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个Ajax Jquery脚本,它应该向php页面发布一个日期数组以获取结果,如果结果正确,它将在页面上的div中显示一条成功消息,如果返回的结果不正确,它将显示一个错误。应该发生的是,每个日期一次发送一个,等待响应后再移动到下一个日期。脚本似乎运行良好,但我注意到日期是以随机顺序返回的。经过调查,我了解到这是因为Ajax调用同时发送多个请求。我想这就是为什么它被称为异步JavaScript和XML(Lol)。无论如何,我已经在这几个小时了,似乎无法找到或理解解决这个问题的方法。我读过一些关

我有一个Ajax Jquery脚本,它应该向php页面发布一个日期数组以获取结果,如果结果正确,它将在页面上的div中显示一条成功消息,如果返回的结果不正确,它将显示一个错误。应该发生的是,每个日期一次发送一个,等待响应后再移动到下一个日期。脚本似乎运行良好,但我注意到日期是以随机顺序返回的。经过调查,我了解到这是因为Ajax调用同时发送多个请求。我想这就是为什么它被称为异步JavaScript和XML(Lol)。无论如何,我已经在这几个小时了,似乎无法找到或理解解决这个问题的方法。我读过一些关于Javascript承诺和各种各样的东西,但我不明白。如果有人能提供一些代码帮助,将不胜感激

以下是包含Javascript和Ajax调用的整个HTML文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dates Range</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-7s5uDGW3AHqw6xtJmNNtr+OBRJUlgkNJEo78P4b0yRw= sha512-nNo+yCHEyn0smMxSswnf/OnX6/KwJuZTlNZBjauKhTK0c+zT+q5JOCx0UFhXQ6rJR9jg6Es8gPuD2uZcYDLqSw==" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-1.12.0.js"></script>
</head>
<script>
    $(function(){ startProcess({"7":"2016-01-07","8":"2016-01-08","9":"2016-01-09","10":"2016-01-10","11":"2016-01-11","12":"2016-01-12","13":"2016-01-13","14":"2016-01-14","15":"2016-01-15","16":"2016-01-16","17":"2016-01-17","18":"2016-01-18","19":"2016-01-19","20":"2016-01-20","21":"2016-01-21","22":"2016-01-22","23":"2016-01-23","24":"2016-01-24","25":"2016-01-25","26":"2016-01-26","27":"2016-01-27","28":"2016-01-28","29":"2016-01-29","30":"2016-01-30","31":"2016-01-31","32":"2016-02-01","33":"2016-02-02","34":"2016-02-03","35":"2016-02-04","36":"2016-02-05"});
        // Ajax to send Date
        function startProcess(arg)
        {
            $.each(arg, function(index, val) 
            {
                $.post('returnsample.php', { query : val }, function(resp) 
                {
                    // resp = $.parseJSON(resp);
                    if (resp == "YES IT WORKED") 
                    {
                        $('.append-range').append('<strong>Date : </strong>'+val+'<p>Got Right Answer</p><br>');
                    }
                    else
                    {
                        $('.append-range').append('<strong>Date : </strong>'+val+'<p>Got error</p><br>');
                    }
                });
            });
        }
    })
</script>
<body>
    <div class="container">
        <div class="row">
            <h1>Range between two dates:</h1>
        </div>
        <br>
                <div class="row">
            <div class="append-range"></div>
        </div>
    </div>
</body>
</html>

日期范围
$(function(){startProcess({“7”:“2016-01-07”,“8”:“2016-01-08”,“9”:“2016-01-09”,“10”:“2016-01-10”,“11”:“2016-01-11”,“12”:“2016-01-12”,“13”:“2016-01-13”,“14”:“2016-01-14”,“15”:“2016-01-15”,“16”:“2016-01-16”,“17”:“2016-01-17”,“18”:“2016-01-18”,“19”:“2016-01-19”,“20”:“2016-01-20”:“2016-01-21”:“2016-01-21”,“2016-01-22”,“2016-01-01”,“24”:”,"25":"2016-01-25","26":"2016-01-26","27":"2016-01-27","28":"2016-01-28","29":"2016-01-29","30":"2016-01-30","31":"2016-01-31","32":"2016-02-01","33":"2016-02-02","34":"2016-02-03","35":"2016-02-04","36":"2016-02-05"});
//发送日期
函数启动进程(arg)
{
$.each(arg,函数(index,val)
{
$.post('returnsample.php',{query:val},函数(resp)
{
//resp=$.parseJSON(resp);
如果(resp==“是,它起作用了”)
{
$('.append range').append('日期:'+val+'得到了正确答案


'); } 其他的 { $('.append range').append('日期:'+val+'出现错误


'); } }); }); } }) 两个日期之间的范围:

只是给你一个想法

1.将异步(ajax)作为回调函数分离

2.从数组中弹出每个项目


3.对异步函数使用promise模式。jquery使用提供promise API。

我所知道的最简单的方法是回调链。我用简单的javascript编写了一个示例,该示例应该会给您一个想法。下面的注释有助于解释

// first, don't use an associative array unless you really need it,
// a regular array will be much easier to iterate
var arg = [ "2016-01-07","2016-01-08","2016-01-09","2016-01-10","2016-01-11","2016-01-12","2016-01-13","2016-01-14","2016-01-15","2016-01-16","2016-01-17","2016-01-18","2016-01-19","2016-01-20","2016-01-21","2016-01-22","2016-01-23","2016-01-24","2016-01-25","2016-01-26","2016-01-27","2016-01-28","2016-01-29","2016-01-30","2016-01-31","2016-02-01","2016-02-02","2016-02-03","2016-02-04","2016-02-05" ];

// start off at index 0
next(arg, 0);

function next(arr, index) {

    // set our terminating condition
    if (index >= arr.length) {
        return;
    }

    // we will set up the next iteration by binding our arguments ahead of time
    var nextCallback = next.bind(null, arr, index + 1);
    // call out to your service, with the current value and a function to process the next once completed
    callService(arr[index], nextCallback);
}

function callService(value, next) {
    // call your service here, I am simulating with setTimeout
    // the key here is that we don't call next() until AFTER we have processed our item
    // $.post(.... function (response) {
    setTimeout(function () {
        // simulate a response since timeout won't provide us one
        var response = value;

        // do stuff with your response
        console.log(response);

        // when done, call the next one
        next();
    }, 100);
}
我创建了一个数组来演示所需的效果。不过,我已将您的对象更改为数组,因为它是数字索引的

脚本所做的是进行ajax调用,当触发
success
时,它将决定要做什么,并重新启动,在整个阵列上循环

u = 0;
function startProcess(arg){
  if(typeof arg[u] !== 'undefined'){
    $.ajax({
      url: "returnsample.php",
      method: 'post',
      query: arg[u],
      success: function(resp){
        if(resp == "YES IT WORKED"){
          $('.append-range').append('<strong>Date : </strong>'+arg[u]+'<p>Got Right Answer</p>')
        }else{
          $('.append-range').append('<strong>Date : </strong>'+arg[u]+'<p>Got error</p>')
        }
        u++;
        startProcess(arg);
      }
    })
  }
}

startProcess(["2016-01-07","2016-01-08","2016-01-09","2016-01-10","2016-01-11","2016-01-12","2016-01-13","2016-01-14","2016-01-15","2016-01-16","2016-01-17","2016-01-18","2016-01-19","2016-01-20","2016-01-21","2016-01-22","2016-01-23","2016-01-24","2016-01-25","2016-01-26","2016-01-27","2016-01-28","2016-01-29","2016-01-30","2016-01-31","2016-02-01","2016-02-02","2016-02-03","2016-02-04","2016-02-05"]);
u=0;
函数启动进程(arg){
if(参数类型[u]!==‘未定义’){
$.ajax({
url:“returnsample.php”,
方法:“post”,
查询:arg[u],
成功:功能(resp){
如果(resp==“是,它起作用了”){
$('.append range').append('日期:'+arg[u]+'得到正确答案

')) }否则{ $('.append range').append('日期:'+arg[u]+'出现错误

')) } u++; 启动过程(arg); } }) } } 启动程序([“2016-01-07”、“2016-01-08”、“2016-01-09”、“2016-01-10”、“2016-01-11”、“2016-01-12”、“2016-01-13”、“2016-01-14”、“2016-01-15”、“2016-01-16”、“2016-01-17”、“2016-01-18”、“2016-01-19”、“2016-01-20”、“2016-01-21”、“2016-01-22”、“2016-01-23”、“2016-01-24”、“2016-01-25”、“2016-01-26”、“2016-01-27”、“2016-01-28”、“2016-01-29”、“2016-01-31”)2016-02-01","2016-02-02","2016-02-03","2016-02-04","2016-02-05"]);

我希望这有帮助

也许不要使用每个循环遍历数组,而是使用一个递增的值递增数组,该值只有在得到想要的结果时才会递增。为什么不发送完整的数组,处理每个日期,然后返回一个结果数组呢?似乎更合理,非常感谢这么快帮助@Vincent。这正是我所需要的。小提琴帮助我看到了它。我非常感谢它。谢谢Kevin。我最终尝试了@Vincent的解决方案。他的小提琴使它很容易看到,而且它与我的原始代码最为相似,但我测试了你的解决方案,它也很有效。我会把它放在我的武器库中!谢谢你的时间,兄弟!