jQuery的AJAX没有返回正常。由于未定义的变量,获取错误“Uncaught SyntaxError:Unexpected token u”。带有多个嵌套的.when内部的jQuery ajax。返回undefined
下面是我的代码和流程。
此方法将上一个按钮,点击它在内部调用具有依赖性的多种方法的情况下调用。在下面的例子中,流量是masterProcess-> buildAndroidApk-> unlockAndroidKey
function masterProcess(thisForm){
$.when(buildAndroidApk()).then(function(result){
obj = JSON.parse(result);
});
}
function buildAndroidApk(){
$.when(unlockAndroidKey()).then(function(result){
obj = JSON.parse(result);
//There are some other .when based on the obj response
return result;
});
}
function unlockAndroidKey(){
//this function connects to server via jQuery Ajax and gets a json string inside success or error block
return '{"success":"1","message":"","content":null}';
}
功能unlockAndroidKey获取JSON字符串,我可以能够接收内部buildAndroidApk。但masterProcess正在接收未定义的字符串,并且JSON.parse导致错误“Unexpected token u”。
我不知道我是否已经解释了我的查询清楚,但是如果需要的话,我可以在更详细的解释。
只是为了理解目的我硬编码我的代码中的json字符串。其实我在PHP中使用数组构建json,并在JavaScript中进行JSON :: parse。你是对的,函数buildAndroidApk()没有返回promise。在我将$ return放在$之前后,现在它正在返回。 – Malaiselvan 2014-12-05 23:38:58
@Malaiselvan - 你明白,当你只有一个承诺时,你不需要'.when()'。你可以直接在promise上使用'.then()'。 – jfriend00 2014-12-06 01:47:01