【问题标题】:How to create multiple objects with django tastypie via Ajax using the PATCH method?如何使用 PATCH 方法通过 Ajax 使用 django sweetpie 创建多个对象?
【发布时间】:2014-05-11 03:11:18
【问题描述】:

尝试使用 Jquery 和它的失败警报 Not connect 创建多个对象,如下面的代码所示。但是使用传递给 ajax 的相同数据 (sent_data) 使用 curl 执行请求,效果很好。

sent_data = {"objects":[{"shopping_id":"#3Q^9%LF728N!9*840H(NEAH2L%J8$3H2J35(ZI@32MSA!S@RD%D3#PQG9^2#*J69S4&7IJX)POV$PYQ70817P2C!6OEPA%*$WR7","quantity":"3","option":"/api/v1/option/2/","item":"/api/v1/item/1/","created_by":"/api/v1/user/-1/","modified_by":"/api/v1/user/-1/","toppings_and_extras":["/api/v1/topping/1/","/api/v1/topping/2/"]},{"shopping_id":"#3Q^9%LF728N!9*840H(NEAH2L%J8$3H2J35(ZI@32MSA!S@RD%D3#PQG9^2#*J69S4&7IJX)POV$PYQ70817P2C!6OEPA%*$WR7","quantity":"3","option":"/api/v1/option/2/","item":"/api/v1/item/1/","created_by":"/api/v1/user/-1/","modified_by":"/api/v1/user/-1/","toppings_and_extras":["/api/v1/topping/1/","/api/v1/topping/2/"]}]};

$.ajax({
        type:'POST',
        url:'http://localhost:8000/api/v1/order/',
        contentType: 'application/json',
        data:sent_data,
        headers:{'X-HTTP-Method-Override':'PATCH'},
        dataType:'json',
        processData:false,
        success: function(data){
            alert("POST request made");

        },
        error: function(jqXHR,exception){
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });

查看django-tastypie PATCH gives me a "400 (Bad Request)" 和Ajax Post Request to TastyPie Doesn't Do Anything 发现,由于我正在尝试进行文档中提到的批量操作,因此我不得不使用“PATCH”方法,我通过提供 X-HTTP-Method-Override 来做到这一点头,但一切都是徒劳的。我可能错过了什么?

【问题讨论】:

    标签: ajax django tastypie


    【解决方案1】:

    在做了更多研究之后,我发现问题是我没有在表单中为“CSRFToken”设置标题,这个要点here 帮助我弄清楚了。所以工作请求看起来像这样,

    sent_data = JSON.stringify({"objects":[{"shopping_id":"#3Q^9%LF728N!9*840H(NEAH2L%J8$3H2J35(ZI@32MSA!S@RD%D3#PQG9^2#*J69S4&7IJX)POV$PYQ70817P2C!6OEPA%*$WR7","quantity":"3","option":"/api/v1/option/2/","item":"/api/v1/item/1/","created_by":"/api/v1/user/-1/","modified_by":"/api/v1/user/-1/","toppings_and_extras":["/api/v1/topping/1/","/api/v1/topping/2/"]},{"shopping_id":"#3Q^9%LF728N!9*840H(NEAH2L%J8$3H2J35(ZI@32MSA!S@RD%D3#PQG9^2#*J69S4&7IJX)POV$PYQ70817P2C!6OEPA%*$WR7","quantity":"3","option":"/api/v1/option/2/","item":"/api/v1/item/1/","created_by":"/api/v1/user/-1/","modified_by":"/api/v1/user/-1/","toppings_and_extras":["/api/v1/topping/1/","/api/v1/topping/2/"]}]});
    
    $.ajax({
            url:'/api/v1/order/?format=json',
            accepts:'application/json',
            contentType: 'application/json',
            headers:{
                'X-HTTP-Method-Override':'PATCH'
            },
            method:'POST',
            data:sent_data,
            beforeSend:function(jqXHR,settings){
                jqXHR.setRequestHeader('X-CSRFToken',$("input[name=csrfmiddlewaretoken]").val())
            },
            dataType:'json',
            processData:false,
            success: function(data){
                alert("damn posted");
    
            },
            error: function(jqXHR,exception){
                if (jqXHR.status === 0) {
                    alert('Not connect.\n Verify Network.');
                } else if (jqXHR.status == 404) {
                    alert('Requested page not found. [404]');
                } else if (jqXHR.status == 500) {
                    alert('Internal Server Error [500].');
                } else if (exception === 'parsererror') {
                    alert('Requested JSON parse failed.');
                } else if (exception === 'timeout') {
                    alert('Time out error.');
                } else if (exception === 'abort') {
                    alert('Ajax request aborted.');
                } else {
                    alert('Uncaught Error.\n' + jqXHR.responseText);
                }
            }
        });
    

    还要注意我传递给了 JSON.stringify() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 2018-09-25
      • 2018-07-31
      • 1970-01-01
      相关资源
      最近更新 更多