阿贾克斯 - JSON犯规补丁只会被发 [英] Ajax - JSON doesnt get sent in PATCH only

查看:96
本文介绍了阿贾克斯 - JSON犯规补丁只会被发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这个从客户端发送JSON数据到我的服务器:

I am trying to send json data from the client to my server using this:

$.ajax({
    url : 'http://127.0.0.1:8001/api/v1/pulse/7/',
    data : data,
    type : 'PATCH',
    contentType : 'application/json'
)};

我收到了无JSON对象可以去codeD 。 然而,当我使用 PUT JSON对象被发送。

I get a No JSON object could be decoded. However when i use PUT the json object gets sent.

它的PATCH只是不工作

后端是Django和使用的应用程序IM是tastypie

The backend is Django and the app im using is tastypie

推荐答案

首先,检查你使用jQuery库的最新版本:

First, check that you use latest version of jQuery library:

  • 在旧版本的直接制约着未知的方法(补丁是新的)。
  • 我jQuery的1.7测试 - 打补丁方法工作没有问题。

其次,并非所有浏览器都支持使用XMLHtt prequest打补丁方法:

Second, not all browsers supports PATCH method using XMLHttpRequest:

  • 像,IE 7,8(9+工作好)有XMLHtt prequest,但将在补丁的错误:

  • Like, IE 7,8 (9+ works okay) have XMLHttpRequest, but it throws an error on PATCH:

new XMLHttpRequest().open('PATCH', '/'); //Illegal argument

  • 要解决这个问题,你可能会迫使jQuery来使用旧的专有的ActiveXObject XHR,像这样:

  • 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();
        }
    });          
    

  • 这篇关于阿贾克斯 - JSON犯规补丁只会被发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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