嵌套Ajax调用和JSON解析

时间:2015-12-11 09:09:01

标签: php jquery json ajax

嵌套的ajax调用会干扰JSON解析吗?

我不这么认为,但我觉得我找到了一些奇怪的东西

SecondJob引发异常解析('意外字符')

//-- Server - Both first.php and second.php
echo json_encode(array('result'=>'ok'));
//---------------

//-- Client (javascript, nested ajax calls) --
// (in class definition)
var self = this; // stores class instance
//
this.FirstJob = function (input) {
  $.ajax({type:'POST', url: 'first.php',
  // Here, response = "\n\n{\"result\": \"ok\"}"
  data: input, success: function(response) {
    var parsed = $.parseJSON(response);
    if(parsed.result==='ok') {
       var forSecond = 'some input';
       self.SecondJob(forSecond);
    }
  },
  error: function(x,s,e) {alert(...);}
}
this.SecondJob = function (input) {
  $.ajax({type:'POST', url: 'second.php',
    // Again, response = "{\"result\": \"ok\"}"
    // This seemingly valid string raises exception
    data: input, success: function(response) {
      var parsed = $.parseJSON(response);
      if(parsed.result==='ok') {
         var forThird = 'some input';
         self.ThirdJob(forThird);
      }
    },
    error: function(x, s, e) {alert(...);}
  )
}
this.ThirdJob = function (input) {
   // Can't go on
}

我经历了常见的错误检查:

  • 在php开始/结束之前/之后没有多余的空格。
  • 所有PHP脚本都保存为Unicode文本。
  • PHP脚本生成的字符串中的键值对都包含在双引号内(没有单引号)

任何可能的情况都会有所帮助。

编辑:主机使用的是PHP 5.2.x,无法更新。

0 个答案:

没有答案