[英]Ajax call inside an ajax success not working
I am trying to make an Ajax call inside another Ajax success function but it somehow doesn't work. 我试图在另一个Ajax成功函数内进行Ajax调用,但它不知何故不起作用。 I get the following error in my console.
我在控制台中收到以下错误。 I don't understand what it means:
我不明白这意味着什么:
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… }
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个...}
I found something like below from the object 我在物体上发现了下面的东西
statusText:"SyntaxError: An invalid or illegal string was specified"
statusText:“SyntaxError:指定了无效或非法字符串”
JS 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 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();
}
}
You are passing an invalid string to type
property inside the makeDecision
function ajax call. 您正在将
makeDecision
函数ajax调用中的无效字符串传递给type
属性。 You should set this as following: 您应该将其设置如下:
type: 'POST',
url: 'engine/main.php',
...
instead of 代替
type: 'engine/main.php'
Make sure that the following line 确保以下行
var obj = JSON.parse(response);
returns an array , not an object. 返回一个数组 ,而不是一个对象。 If
obj
is not an array, then obj.length
is undefined
如果
obj
不是数组,则obj.length
undefined
You can call ajax again inside the ajaxComplate event like this: 你可以在ajaxComplate事件中再次调用ajax,如下所示:
$( document ).ajaxComplete(function() {
$.ajax({
type: "post",
url: 'engine/main.php',
data: {code: 3},
success: function(winner) {
console.log('end');
console.log(winner);
},
error: function(data) {
console.log(data);
}
});
});
You can not get length of object directly obj.length
in javascript.So if you are getting response properly.Get length of Parsed object like this.. 你不能直接在javascript中获取对象的长度
obj.length
如果你得到正确的响应。获得这样的Parsed对象的长度..
var length = Object.keys(obj).length;
Then 然后
lastClick = obj[length-1].player;
Make correct use of $.each
like this.. 正确使用
$.each
就像这样..
$.each(obj,function (i, val){
//code
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.