Javascript Ajax - JSON 不会仅在 PATCH 中发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11461414/
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 - JSON doesnt get sent in PATCH only
提问by nknj
I am trying to send json data from the client to my server using this:
我正在尝试使用以下方法将 json 数据从客户端发送到我的服务器:
$.ajax({
url : 'http://127.0.0.1:8001/api/v1/pulse/7/',
data : data,
type : 'PATCH',
contentType : 'application/json'
)};
I get a No JSON object could be decoded
.
However when i use PUT
the json object gets sent.
我得到一个No JSON object could be decoded
. 但是,当我使用PUT
json 对象时,它会被发送。
It only doesnt work for PATCH
它只对 PATCH 不起作用
The backend is Django and the app im using is tastypie
后端是Django,我使用的应用程序是tastypie
回答by vitrilo
First, check that you use latest version of jQuery library:
首先,检查您是否使用了最新版本的 jQuery 库:
- Older versions directly restrict unknown methods (PATCH is new one).
- I've tested on jQuery 1.7 - PATCH method working without problems.
- 旧版本直接限制未知方法(PATCH 是新的)。
- 我已经在 jQuery 1.7 - PATCH 方法上测试过没有问题。
Second, not all browsers supports PATCH method using XMLHttpRequest:
其次,并非所有浏览器都支持使用 XMLHttpRequest 的 PATCH 方法:
Like, IE 7,8 (9+ works okay) have XMLHttpRequest, but it throws an error on PATCH:
new XMLHttpRequest().open('PATCH', '/'); //Illegal argument
To fix this, you may force jQuery to use the old proprietary ActiveXObject xhr, like so:
$.ajax({ url : 'http://127.0.0.1:8001/api/v1/pulse/7/', data : data, type : 'PATCH', contentType : 'application/json', xhr: function() { return window.XMLHttpRequest == null || new window.XMLHttpRequest().addEventListener == null ? new window.ActiveXObject("Microsoft.XMLHTTP") : $.ajaxSettings.xhr(); } });
比如,IE 7,8(9+ 工作正常)有 XMLHttpRequest,但它在 PATCH 上抛出一个错误:
new XMLHttpRequest().open('PATCH', '/'); //Illegal argument
要解决此问题,您可以强制 jQuery 使用旧的专有 ActiveXObject xhr,如下所示:
$.ajax({ url : 'http://127.0.0.1:8001/api/v1/pulse/7/', data : data, type : 'PATCH', contentType : 'application/json', xhr: function() { return window.XMLHttpRequest == null || new window.XMLHttpRequest().addEventListener == null ? new window.ActiveXObject("Microsoft.XMLHTTP") : $.ajaxSettings.xhr(); } });
回答by turtlemonvh
A bit late, but this worked for me when I got this error:
有点晚了,但是当我收到此错误时,这对我有用:
$.ajax({
url : 'http://127.0.0.1:8001/api/v1/pulse/7/',
data : JSON.stringify(data),
type : 'PATCH',
contentType : 'application/json',
processData: false,
dataType: 'json'
});
Serializing the object yourself instead of letting jQuery do it seems to help. This works for me on the latest version of Chrome, but still doesn't fix the ie problems mentioned in other responses.
自己序列化对象而不是让 jQuery 这样做似乎有帮助。这在最新版本的 Chrome 上对我有用,但仍然无法解决其他回复中提到的 ie 问题。
回答by nknj
var request = new XMLHttpRequest();
request.open('PATCH', 'http://127.0.0.1:8001/api/v1/pulse/6/', false);
request.setRequestHeader("Content-type","application/json");
request.send('{"isActive": 1}');
Using a an XMLHttpRequest solves it!
使用 XMLHttpRequest 可以解决它!