嵌套的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 5.2.x,无法更新。