Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX call error - status of 400 (Bad Request)

I'm trying to use the BloomAPI to retrieve Doctor's NPI number by querying with their first and last name. I'm using Jquery Ajax to make a get request for the JSON data.

I am able to get the JSON data when I do CURL in the terminal: curl -X GET 'http://www.bloomapi.com/api/search?offset=0&key1=last_name&op1=eq&value1=LIN&key2=first_name&op2=eq&value2=JOHN'

For the purpose below - I just hardcoded in the params into the URL. I get a "Failed to load resource: the server responded with a status of 400 (Bad Request" Error. Any idea what I might be doing wrong?

$.ajax({
    type: 'GET',
    url: 'http://www.bloomapi.com/api/search?offset=0&key1=last_name&op1=eq&value1=LIN&key2=first_name&op2=eq&value2=JOHN',
   dataType: 'jsonp'
  }).done(function(server_data) {
    console.log(server_data)
  }).fail(console.log("failed"));
like image 911
john c Avatar asked Nov 15 '25 03:11

john c


1 Answers

This was a weird one... your code is actually basically correct, however, it appears bloomapi does not support disabling caching in the way jquery does it.

When you make the jquery call you have, the actual url becomes something like this:

http://www.bloomapi.com/api/search?offset=0&key1=last_name&op1=eq&value1=LIN&key2=first_name&op2=eq&value2=JOHN&callback=jQuery111207365460020955652_1428455335256&_=1428455335257

The callback is a jsonp construct, and the _ is a way of breaking caching. However, bloomapi appears to not like this:

jQuery111207365460020955652_1428455335256({"name":"ParameterError","message":"_ are unknown parameters","parameters":{"_":"is an unknown parameter"}});

To get around this, you can disable cache busting like so:

$.ajax({
    type: 'GET',
    url: 'http://www.bloomapi.com/api/search?offset=0&key1=last_name&op1=eq&value1=LIN&key2=first_name&op2=eq&value2=JOHN',
    dataType: 'jsonp',
    cache: true
}).done(function(server_data) {
    console.log(server_data)
}).fail(function() { console.log("failed") });

You will have to be careful of how else you break the cache if that's an issue; the api provider may be able to provide feedback on how to do this.

In the future, you can easily check the errors you are receiving/what you are sending using a web debugger; I used Fiddler to figure this out.

like image 108
Brian Avatar answered Nov 17 '25 18:11

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!