最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to get data from ajax call? - Stack Overflow

matteradmin6PV0评论

I am trying to get data from ajax call by cross domain. Here is code

function GetMaxWULen() {
var x;
$.ajax({
    url : url,
    method : 'POST',
    jsonp : "callback",
    async : false,
    data : {
        Function : "GetMaxWULen",
        Authorization : Base64.encode(login + ":" + token),
        WuType : $("#ddlWUType").val()
    },
    dataType : 'jsonp',
    crossDomain : true,
    error : function(request, status, error) {
        alert('nie udało sie');
        alert(error);
    }
}).done(function(result) {
    console.log('done result');
    x = result;
    console.log(x);
});
console.log('function end');
console.log(x);}

At the end of the function, x variable is undefined but in done event value is correct. Could anyone can help me or tell what is wrong in this code?

I am trying to get data from ajax call by cross domain. Here is code

function GetMaxWULen() {
var x;
$.ajax({
    url : url,
    method : 'POST',
    jsonp : "callback",
    async : false,
    data : {
        Function : "GetMaxWULen",
        Authorization : Base64.encode(login + ":" + token),
        WuType : $("#ddlWUType").val()
    },
    dataType : 'jsonp',
    crossDomain : true,
    error : function(request, status, error) {
        alert('nie udało sie');
        alert(error);
    }
}).done(function(result) {
    console.log('done result');
    x = result;
    console.log(x);
});
console.log('function end');
console.log(x);}

At the end of the function, x variable is undefined but in done event value is correct. Could anyone can help me or tell what is wrong in this code?

Share Improve this question asked Oct 30, 2013 at 11:22 dawiddawid 291 gold badge1 silver badge2 bronze badges 4
  • It's asyncronous, so that last line of code is executed before the request loads, therefore x doesn't have a value yet. It works in the done event because that waits for the request to load before executing the function. – Joe Simmons Commented Oct 30, 2013 at 11:25
  • above error: add success:YourfunctionName(), later then YourfunctionName(response ){alert(response)} – Satinder singh Commented Oct 30, 2013 at 11:26
  • 2 possible duplicate of How to return the response from an AJAX call? – Rory McCrossan Commented Oct 30, 2013 at 11:27
  • 1 Please use the search feature. This question has been asked hundreds of times. – Rory McCrossan Commented Oct 30, 2013 at 11:27
Add a ment  | 

2 Answers 2

Reset to default 3

This happens because your AJAX request is done asynchronously. It means the rest of your code won't wait your response be ready to continue.

If you need to use the data returned from AJAX outside your function, you might want to create a parameter to serve as a callback when the response is ready. For example:

function yourFunction(callback) {
    $.ajax({
        /* your options here */
    }).done(function(result) {
        /* do something with the result here */
        callback(result); // invokes the callback function passed as parameter
    });
}

And then call it:

yourFunction(function(result) {
    console.log('Result: ', result);
});

Fiddle: http://jsfiddle/9duek/

try

$.ajax({
    url : url,
    method : 'POST',
    jsonp : "callback",
    async : false,
    data : {
        Function : "GetMaxWULen",
        Authorization : Base64.encode(login + ":" + token),
        WuType : $("#ddlWUType").val()
    },
    dataType : 'jsonp',
    crossDomain : true,
    error : function(request, status, error) {
        alert('nie udało sie');
        alert(error);
    }
}).success(function(result) {
    var datareturned = result.d;
    console.log('done' +  datareturned);
    x = datareturned;
    console.log(x);
});
Post a comment

comment list (0)

  1. No comments so far