0

I need to call a function which does async ajax call and the request is cross domain,

At jQuery documentation is written:

async (default: true) Type: Boolean By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation

I saw that there is $.when but the function itself exits before the ajax request comes back.

function checkLoginData() {
    var jsonData = JSON.stringify({"db": db, "username": username, "password": password});
    var response;
    $.ajax({
           type: "POST",
           url: "http://someURL/checkLogin.php",
           data: {data: jsonData},
           success: function (data) {
              response = data;
              if (response == '"Error"') {
                 return false;
              } else {
                return true;
              }
          },
          error: function (jqXHR, textStatus, errorThrown) {
             return false;
         }
    });
};

I'm not really familiar with promises and I would really appreciate if someone knows how to make this work

This function is boolean

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pini Cheyni
  • 5,073
  • 2
  • 40
  • 58
  • Where is the `when`? You can not return from an asynchronous method. Read this: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – epascarello Jan 25 '16 at 13:02
  • `the request is cross domain` - besides the returning from async call duplicate, you may have a CORS issue as well – Jaromanda X Jan 25 '16 at 13:03
  • I tried to use it at the end of this function and save the whole $.ajax into variable and then run the $.when on this variable but it didn't work, I still exited the function before I got response – Pini Cheyni Jan 25 '16 at 13:04

0 Answers0