Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX: only error callback is been fired

I have declared success and error callbacks, but in case of status code 200 also it calls error callback only.

I have been making curl call to some other php file too inside registry.php.

Here what i tried:

$.ajax({
  type: "POST"
  ,data: {
    action: "blah"
    ,mobileNo: that.mobNo
    ,token: that.key
  }
  ,url: "http://90.8.41.232/localhost/registry.php"
  ,cache: false
  ,dataType: "json"
  ,success: function (response) {
    console.log("success");
  }
  ,error: function () {
    console.log("error");
  }
});

I have read in documentation that we don't have to call success callback explicitly, hope it's correct.

Any idea how to call success callback when it is 200 status code.

RESPONSE hope this will help, this I copied from chrome console, not printed by console.log().

bort: (statusText)
always: ()
complete: ()
done: ()
error: ()
fail: ()
getAllResponseHeaders: ()
getResponseHeader: (key)
overrideMimeType: (type)
pipe: ()
progress: ()
promise: (obj)
readyState: 4
responseText: "{"success":"Mobile No 9535746622 is approved"}      {"report":true}"
setRequestHeader: (name,value)
state: ()
status: 200
statusCode: (map)
statusText: "OK"
success: ()
then: ()
like image 933
PJ1405 Avatar asked Aug 19 '15 12:08

PJ1405


2 Answers

As you have mentioned that you are making a curl call to some other php, what happens is whenever you make curl call you have to return the response of the curl to the client. So to transfer the return value of curl call, you have to set an option curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);. This will sort your problem. Hope so.

like image 110
Raghavendra Shivhare Avatar answered Oct 06 '22 01:10

Raghavendra Shivhare


First, checkout the real error you get (not certain you really get a 200 status ?)

error: function (err) {
  console.error("error");
  console.log(err);
  console.log(err.stack);
  throw err;
}

Show us the resulting log please.

like image 22
Rémi Becheras Avatar answered Oct 06 '22 01:10

Rémi Becheras