javascript - 使用 AJAX 在 javaScript 中接收到错误数据

标签 javascript jquery asynchronous call

我在 file.js 中进行了多次 AJAX 调用,调用具有以下形式:

file.js

$.post('functions.php',{consulta: 'tbl_Cours', action: {"ask_query_field":variable1}).val()}, function(res1){ 
  if (res1){
    //do something
  } 

  //Second call
  $.post('functions.php',{consulta: 'tbl_Cours', action: {"ask_query_field":variable2}}, function(res2){ 
    if(res2){
      //do something
    } 

    //third call
    $.post('functions.php',{consulta: 'tbl_Cours', action: {"ask_query_field":variable3}}, function(res3){ 
      if(res3){
        console.log(res3);
      } 
    });
  });
}); 

因此,第一次和第二次调用返回一切都很好,我调用相同的文件 functions.php 和相同的函数。但是在第三次调用中我得到了一个数据,但它与从服务器 server php 发送的数据不同。

我看到 php 中的日志并显示返回此:

log from php

[30-Jan-2018 11:06:50 America/Chicago] Array
(
  [tema] => Tema 1 .- Introducción y definiciones
  [pk_tema] => 3
  [cantidad] => 1
)

[30-Jan-2018 11:06:50 America/Chicago] Array
(
  [tema] => Tema 2 .- Posiciones y valoraciones
  [pk_tema] => 4
  [cantidad] => 3
)

[30-Jan-2018 11:06:50 America/Chicago] Array
(
  [tema] => Tema 3 .- Practicas y actividades
  [pk_tema] => 5
  [cantidad] => 3
)

接下来,在 Javascript 中我看到我收到的内容,并且数据不同

>>  {0: {…}, 1: {…}, 2: {…}}
0:{tema: "Tema 1 .- Introducción y definiciones", pk_tema: "3", cantidad: "1"}
1:{tema: "Tema 2 .- Posiciones y valoraciones", pk_tema: "4", cantidad: "4"}
2:{tema: "Tema 3 .- Practicas y actividades", pk_tema: "5", cantidad: "3"}

pk_tema:4 现在有 cantidad :4,之前在functions.js中有3。 我已经尝试过

  1. 将变量名称更改为result 例如:res3 更改为data_received,结果是一样的。
  2. Put $.ajaxSetup({async: false});因为我认为这是同步问题。但结果是一样的。
  3. 我通过控制台执行了第三次调用,结果很好。

所以,我不确定我的问题是什么,我使用 php7 和 jQuery v1.12.4。在 Google Chrome 中

最佳答案

感谢@bigless,因为我没有检查开发人员的所有工具,我在JavaScript中收到了正确的数据,但我不确定为什么它会随着时间而变化,我永远不会使用两次变量 res3但是它的信息发生了变化。非常感谢您抽出时间。

0:{tema: "Tema 1 .- Introducción y definiciones", pk_tema: "3", cantidad: "1"}
1:{tema: "Tema 2 .- Posiciones y valoraciones", pk_tema: "4", cantidad: "3"}
2:{tema: "Tema 3 .- Practicas y actividades", pk_tema: "5", cantidad: "3"}

关于javascript - 使用 AJAX 在 javaScript 中接收到错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529720/

相关文章:

javascript - 在使用 HTTP/2 时,缩小和连接 JS/CSS 文件以及对图像使用 sprite 是否仍然提供性能优势?

javascript - 是否可以在 Chromium OS 而不是 Chromium 上构建 Electron 应用程序?

javascript - 将用户输入的原始 JSON 输入到 JSON 对象中

一个范围内的当前时间指示的jquery帮助

javascript - 如何使用 jquery 将 json 普遍解析为 block ?

javascript - 使用node.js async 和 require 获取多个url

multithreading - 如何从非 tcl 线程异步调用 Tcl 线程中的命令?

javascript - XmlHttpRequest onprogress 间隔

javascript - 当 JavaScript 是单线程语言时,如何在 JavaScript 中实现异步非阻塞代码?

Jquery 在 Chrome 中不工作,在 Safari 和 Firefox 中不符合预期