-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} 
11
  • duplicate of stackoverflow.com/questions/16744066/… Commented Feb 9, 2016 at 17:27
  • "not callbacks such as success, done, always, and error" — Three of those aren't appearing in the code in your question. Commented Feb 9, 2016 at 17:30
  • tried adding the datatype: 'json', still not working Commented Feb 9, 2016 at 17:31
  • if(!err){ console.log(err); doesn't really make any sense at all. Commented Feb 9, 2016 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. Commented Feb 9, 2016 at 17:33

1 Answer 1

-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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.