我在从ajax-call函数 getRequest 获取变量值时遇到问题并将其置于函数测试中,不确定它是如何工作的,任何帮助都表示赞赏!
function test() {
var out = getRequest().name; //problem
console.log(out);
};
function getRequest() {
$.ajax({
url: '/nextdocument',
type: 'GET',
async: true,
cache: false,
timeout: 11000, //vänta på svar från servern om ingen inläsning
success: function(data) {
var name = data.description;
var price = data.price;
console.log("read--> " + name + price);
setTimeout(
'getRequest()',
1000
);
}
})
}
答案 0 :(得分:2)
function test() {
var XHR = getRequest();
XHR.done(function(data) {
var out = data.description;
console.log(out);
});
}
function getRequest() {
var XHR = $.ajax({
url: '/nextdocument',
type: 'GET',
async: true,
cache: false,
timeout: 11000, //vänta på svar från servern om ingen inläsning
setTimeout(getRequest, 1000); //WHY
});
return XHR;
}