我想在另一個Ajax成功函數內進行Ajax調用,但它以某種方式不起作用。我的控制檯中出現以下錯誤。我不明白這是什麼意思:阿賈克斯調用裏面的ajax成功不起作用
Object { readyState: 0, getResponseHeader: .ajax/v.getResponseHeader(), getAllResponseHeaders: .ajax/v.getAllResponseHeaders(), setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType: .ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 10 more… }
我發現類似下面從對象
statusText:"SyntaxError: An invalid or illegal string was specified"
JS
//Update the board with the moves so far made
var updateBoard = function() {
var style;
$.ajax({
type: "POST",
url: "engine/main.php",
data: {code: 2},
success: function(response) {
if(response != "") {
var obj = JSON.parse(response);
lastClick = obj[obj.length - 1].player;
$(obj).each(function (i, val) {
if (val.player == 1) {
style = "cross";
}
else if (val.player == 2) {
style = "circle";
}
$('td[data-cell="' + val.cell + '"]').html(val.sign).addClass(style);
});
if(obj.length > 2) {
makeDecision();
}
}
else {
lastClick = null;
$('td').html("").removeClass();
}
setTimeout(updateBoard, 1000);
}
});
};
updateBoard();
function makeDecision() {
console.log('starting decision function');
$.ajax({
type: "engine/main.php",
data: {code: 3},
success: function(winner) {
console.log('end');
console.log(winner);
},
error: function(data) {
console.log(data);
}
});
}
PHP
if(isset($_POST['code'])) {
$code = $_POST['code'];
//Handle player number on game start
if($code == 1) {
if (!isset($_COOKIE['gamePlay'])) {
header('Location: index');
}
$playerCode = $_COOKIE['gamePlay'];
$player = $playersHandler->getPlayer($playerCode);
echo $player;
}
// Update board with new moves
else if($code == 2) {
$currentPosition = $gameHandler->getMoves();
echo $currentPosition;
}
else if($code == 3) {
$result = $code; //$gameHandler->decide();
echo $result;
}
//Reset Board
else if($code == 4) {
$gameHandler->reset();
}
}
'console.log(response);'輸出什麼? –
你的意思是在第一次ajax成功電話?然後它是一個JSON數組。 – Ayan
哪條線發生錯誤?可能是'response'不是有效的JSON,'val.cell'在jQuery選擇器中無效,或者'val.sign'不是有效的HTML。 – dave