Search code examples
javascriptarraysajaxpolymer-2.x

get the return value of success in Iron-Ajax after POST


I'm currently working the project using Polymer and I'd like to get the return value of API after POST using Iron-Ajax.

Here is my sample code,

var rs = $.ajax({
    type: "POST",
    url: apiUrl,
    data: _data,
    dataType: "json",
    contentType: 'application/json'
});

rs.done(function (data) {
    console.log(data);
    alert(data);
    }
});

Solution

  • Ajax is asynchronous by default,you need add async:false to make it synchronous

    var rs = $.ajax({
        type: "POST",
        url: apiUrl,
        data: _data,
        async:false,
        dataType: "json",
        contentType: 'application/json'
    });
    
    var result = null;
    rs.done(function (data) {
        console.log(data);
        alert(data);
        result = data;
        }
    });
    
    //return result;//you can return value like this