且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AJAX呼叫错误-状态为400(错误请求)

更新时间:2022-06-27 07:05:46

这是一个很奇怪的...您的代码实际上是正确的,但是,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.