Reputation: 1331
I'm calling an API and getting results back as expected in JavaScript. If a 400 status is returned, I call another API, which I'm doing with an if statement. However, this "if" is never triggered, I think because the way my response is getting read. My code is:
var getSearchPrimary = {
url: 'SEARCH_URL',
type: 'POST',
crossDomain: true,
dataType: 'jsonp',
context : this,
cache : true,
timeout: 60000,
success : function(e) {
status = JSON.stringify(e.status.code);
if(status === "400"){
console.log("failed");
this.getSearchSecondary(query);
}else{
console.log(JSON.stringify(e));
}
In this case, when I do console.log(status);
I get "400"
so I'm really not sure why my if statement is never firing!
The response is:
{"status":{"code":"400","message":"Error: No results match the query"},"response":{"results":""}}
I've been staring at this a while now, and would really appreciate any insight!!
Upvotes: 0
Views: 83
Reputation: 20236
The "success" method is called when the request is successful. When 400 is returned, that's not a success. Use the "error" property instead to provide a function that is executed when the request fails.
Upvotes: 1