-1

I have the following js function, I am having a problem where the success callback is never being called. I stepped through the relevant JQuery code in chrome debugger, and it is picking up the header object in the options argument, but not callbacks such as success, done, always, and error. It just skips them, and I have no idea why. I've tried specifying the url as a entry in options, and it is picked up as I would expect.

Forex.getInstrumentList = function(){
  var response;
  $.ajax("https://api-fxpractice.oanda.com/v1/instruments?accountId=" + Forex.selectedAcct,
  {

    success: function(data, err, jqXHR){
      if(err){
        console.log(err);
        return;
      }
      response = data;
    },

    headers: {
      Authorization: "Bearer notMyrealkey345234234wstd3451345"
    }
  });

  return response; //JSON
}

I grabbed the following from the debugger, as you can see, the success function isn't even in the options argument. I have no idea where or how to start fixing this, so any help is appreciated!

url , and options argument values for the $.ajax function

url = "https://api-fxpractice.oanda.com/v1/instruments?accountId=4161836", options = Object {headers: Object}
Cayle
  • 199
  • 1
  • 1
  • 11
  • duplicate of http://stackoverflow.com/questions/16744066/jquery-ajax-done-function-not-firing – Darshan Feb 09 '16 at 17:27
  • "not callbacks such as success, done, always, and error" — Three of those aren't appearing in the code in your question. – Quentin Feb 09 '16 at 17:30
  • tried adding the `datatype: 'json'`, still not working – Cayle Feb 09 '16 at 17:31
  • `if(!err){ console.log(err);` doesn't really make any sense at all. – Quentin Feb 09 '16 at 17:32
  • I've tried doing all of those, none of them worked. I only needed success, so I posted with the barebones version. – Cayle Feb 09 '16 at 17:33
  • If the code isn't working, then you need a success handle which *always* gives some output when it runs and if it outputs nothing you also need an error handler. That's how you figure out what the error is. – Quentin Feb 09 '16 at 17:33
  • @Quentin Yeah, I spotted that weird construction too. – Michelangelo Feb 09 '16 at 17:34
  • I've tried using all of those in several combinations. The error isn't with the contents of the callback it's just not picking up the callbacks at all. It will pick up the header, and datatype entries in options, but not success. – Cayle Feb 09 '16 at 17:35
  • Maybe you have an `ajaxSetup` handler that's removing it? – Barmar Feb 09 '16 at 17:44
  • Press F12 and go to the network tab. Is a request being sent? what status code is the request responding with? – Kevin B Feb 09 '16 at 19:40
  • Yeah, I answered my question below. It's async so it doesn't necessarily step through the success function in sequence. I changed it to a synchronous request – Cayle Feb 09 '16 at 19:47

1 Answers1

-1

So what's happening is that jQuery doesn't read the callbacks until they need to be run. Since it's an AJAX call and async is true by default, the debugger would step through it while it was performing the web request and then only go back to the success function AFTER the request is completed. If you set the async option to false then it's fixed as it will block until you actually get the response back. If you don't, it will return null as the success function hasn't run, and therefore hasn't modified the value of response

Cayle
  • 199
  • 1
  • 1
  • 11