2
我是jquery的新手我有一个php返回一个json,所以我可以从jquery获得它,但是有一个问题得到结果。从jquery解析json的问题
这里是我的代码:
calculate: function(me, answer, res_id, soulmates) {
console.log('CALCULATE: ');
var deferred = $.Deferred();
data = {
'me': me,
'answer': answer,
'resid': res_id,
};
$.ajax({
url: appConfig.calculate_url,
type: 'post',
beforeSend: function() {
console.log('BEFORE');
Site.switch_calculation_animations();
console.log('AFTER');
console.log(appConfig.calculate_url);
},
data: JSON.stringify(data),
timeout: 15000
}).done(function(ans) {
console.log(ans);
console.log(ans.ok);
console.log(ans.combi_id);
console.log(ans.slug);
if (ans.ok == 'yes') {
console.log('YES');
deferred.resolve(ans);
}
}).fail(function(jqXHR, textStatus, error) {
console.log('ERROR');
Site.handle_exception('calculate', {
'textStatus': textStatus,
'error': error
});
deferred.reject();
});
console.log('END CALCULATE');
return deferred.promise();
},
控制台日志我得到的是:
CALCULATE:
app.js?v=35:242 BEFORE
app.js?v=35:244 AFTER
app.js?v=35:245 /es/test_calculate/4170/waiting/
app.js?v=35:266 END CALCULATE
app.js?v=35:250 {"ok":"yes","combi_id":6059244666,"slug":"true"}
app.js?v=35:251 undefined
app.js?v=35:252 undefined
app.js?v=35:253 undefined
因此,尽管确定值是 “yes”,不进入if命令。为什么?我错过了什么?
感谢
右下方'data',并正上方'timeout',加上'数据类型: 'JSON',' – adeneo