我有问题,异步禁用ajax。我有以下代码:
function GetDataFromUninorte() {
link="http://www.uninorte.edu.co/documents/71051/11558879/ExampleData.csv/0e3c22b1-0ec4-490d-86a2-d4bc4f512030";
var result=
$.ajax({
url: 'http://whateverorigin.org/get?url=' + link +"&callback=?" ,
type: 'GET',
async: false,
dataType: 'json',
success: function(response) {
console.log("Inside: " + response);
}
}).responseText;
console.log("Outside: "+result);
return result;
}我得到了以下结果:
正如您所看到的,“外部”总是首先运行,结果是未定义的,不能处理数据。
我已经试过了
当..。然后
异步= false
以参数I/O函数传递数据
其他的东西,但什么都没有
:/
..。事先非常感谢
(我不是以英语为母语的人,如果我写得不好,我很抱歉)
解决了
也许不是最好的形式,但在"success:“语句中,我调用了一个接收ajax响应并触发进程其余部分的函数,这样我就不需要将其存储在变量中,异步也不影响我。
发布于 2016-06-17 05:31:35
用这个:
function GetDataFromUninorte() {
link="http://www.uninorte.edu.co/documents/71051/11558879/ExampleData.csv/0e3c22b1-0ec4-490d-86a2-d4bc4f512030";
var result=
$.ajax({
url: 'http://whateverorigin.org/get?url=' + link +"&callback=?" ,
type: 'GET',
**timeout: 2000,**
async: false,
dataType: 'json',
success: function(response) {
console.log("Inside: " + response);
}
}).responseText;
console.log("Outside: "+result);
return result;
}发布于 2016-06-17 05:32:29
使用可以使用回调,可以读取更多的这里。
function GetDataFromUninorte(successCallback, errorCallback) {
link="http://www.uninorte.edu.co/documents/...";
$.ajax({
url: 'http://whateverorigin.org/get?url=' + link +"&callback=?" ,
type: 'GET',
async: false,
dataType: 'json',
success: successCallback,
error: errorCallback
});
}
function mySuccessCallback(successResponse) {
console.log('callback:success', successResponse);
}
function myErrorCallback(successResponse) {
console.log('callback:success', successResponse);
}
GetDataFromUninorte(mySuccessCallback, myErrorCallback);或者您可以使用承诺 (IE浏览器中的错误支持)
function GetDataFromUninorte() {
return new Promise(function(resolve, reject) {
link="http://www.uninorte.edu.co/documents/...";
$.ajax({
url: 'http://whateverorigin.org/get?url=' + link +"&callback=?" ,
type: 'GET',
async: false,
dataType: 'json',
success: resolve,
error: reject
});
});
}
GetDataFromUninorte()
.then(function(successResponse){
console.log('promise:success', successResponse);
}, function(errorResponse){
console.log('promise:error', errorResponse);
});发布于 2016-06-17 05:33:02
AJAX本质上是asynchronous,您需要传递callback,当接收到ajax响应时将调用它。然后,您可以从responseText对象访问xhr。
您还可以使用jQuery Deferred和promise来解决您的问题,如下所示:
function GetDataFromUninorte() {
var defObject = $.Deferred(); // create a deferred object.
link="http://www.uninorte.edu.co/documents/71051/11558879/ExampleData.csv/0e3c22b1-0ec4-490d-86a2-d4bc4f512030";
$.ajax({
url: 'http://whateverorigin.org/get?url=' + link +"&callback=?" ,
type: 'GET',
async: false,
dataType: 'json',
success: function(response) {
console.log("Inside: " + response);
defObject.resolve(response); //resolve promise and pass the response.
}
});
return defObject.promise(); // object returns promise immediately.
}然后:
var result = GetDataFromUninorte();
$.when(result).done(function(response){
// access responseText here...
console.log(response.responseText);
});您应该通过设置synchronous来避免AJAX async:false,因为这将阻止用户界面上的进一步交互。
https://stackoverflow.com/questions/37873684
复制相似问题