AJAX呼叫错误-状态为400(错误请求) [英] AJAX call error - status of 400 (Bad Request)

查看:428
本文介绍了AJAX呼叫错误-状态为400(错误请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用BloomAPI通过查询医生的名字和姓氏来检索Doctor的NPI号码.我正在使用Jquery Ajax对JSON数据进行get请求.

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.

当我在终端中执行CURL时,我能够获取JSON数据:curl -X GET'

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'

出于以下目的-我只是将参数中的硬编码到URL中. 我收到无法加载资源:服务器响应,状态为400(错误请求),错误.任何想法我可能做错了吗?

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"));

推荐答案

这是一个很奇怪的...您的代码实际上是正确的,但是,bloomapi似乎不支持以jquery的方式禁用缓存.

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.

当您进行jquery调用时,实际的URL会变成这样:

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

回调是jsonp构造,而_是中断缓存的一种方式.但是,bloomapi似乎不喜欢这样:

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") });

如果这是一个问题,您将必须小心如何破坏高速缓存; api提供程序可能能够提供有关如何执行此操作的反馈.

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.

将来,您可以使用网络调试器轻松检查收到的错误/发送的错误;我用Fiddler弄清楚了.

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.

这篇关于AJAX呼叫错误-状态为400(错误请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆