I'm trying to get json form other domain, my code is as below:
var token = '';
function getData(){
console.log("get data suc");
for (var i=0; i < urls.length; i++){
var code = 'http://example.com/api/'+ urls[i];
$.ajax({
async: false,
url: code,
type: 'GET',
dataType: 'jsonp',
success: function(data) { showData(data); },
error: function() { console.log('ajax Failed!'); },
beforeSend: setHeader,
});
}
}
function showData(data){
$("<tr></tr>").append("<td>" + data + "</td>")
.appendTo("#results");
console.log(data);
}
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', token);
}
This result should be display as order the key I gave from array urls. ex: urls = [1,2,3,4]→should get [one,two,three,four] but I always get the wrong order!(ex: [two,one,three,four] or [three,four,two,one]) What happened? Is that "async: false" didn't work? And why?