Javascript AJAX 调用错误 - 状态为 400(错误请求)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29503984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AJAX call error - status of 400 (Bad Request)
提问by john c
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.
我正在尝试使用 BloomAPI 通过查询医生的名字和姓氏来检索医生的 NPI 编号。我正在使用 Jquery Ajax 对 JSON 数据发出 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'
当我在终端中执行 CURL 时,我能够获取 JSON 数据: 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?
出于以下目的 - 我只是将参数硬编码到 URL 中。我收到“加载资源失败:服务器响应状态为 400(错误请求”)错误。知道我做错了什么吗?
$.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"));
回答by Brian
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.
这是一个奇怪的...您的代码实际上基本上是正确的,但是,bloomapi 似乎不支持以 jquery 的方式禁用缓存。
When you make the jquery call you have, the actual url becomes something like this:
当您进行 jquery 调用时,实际的 url 变为如下所示:
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:
回调是一个 jsonp 结构,_ 是一种破坏缓存的方式。但是,bloomapi 似乎不喜欢这样:
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.
如果这是一个问题,您将不得不小心如何破坏缓存;api 提供者可能能够提供有关如何执行此操作的反馈。
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.
将来,您可以使用 Web 调试器轻松检查收到的错误/发送的错误;我使用 Fiddler 来解决这个问题。

