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

I am trying to get data from ajax call by cross domain.Here is codefunction GetMaxWULen() {var x;$.a

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);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745672036a4639463.html

相关推荐

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

    I am trying to get data from ajax call by cross domain.Here is codefunction GetMaxWULen() {var x;$.a

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信