尝试使用Jquery及其失败警报Not connect
使用django tastypie创建多个对象,如下面的代码所示。但使用相同的数据(sent_data
)执行curl请求传递给ajax,效果很好。
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标题但都是徒劳的。我能错过什么?
答案 0 :(得分:0)
在做了更多的研究后,我发现问题是我没有为表单中的'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()方法。