试图做一个帖子在阿贾克斯与Tastypie时,我得到一个401。我可以将GET记录到控制台,并使用相同的身份验证。我该如何调试?我的tastypie POST请求有什么问题?
这里是我的javascript:
// sending a csrftoken with every ajax request
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
crossDomain: true, // obviates need for sameOrigin test
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
}
}
});
//this is the get request, which works fine
$.ajax({
url: "http://localhost:8080/data/api/v1/user/?format=json",
success: function(data){
console.log(data);
}
});
$(function() {
$('#newEntry').click(function() {
var table=$("#entryName").val();
var d = JSON.stringify({
"name":entry,
"user":"http://localhost:8080/data/api/v1/user/?format=json"
});
$.ajax({
type: 'POST',
data: d,
success: function(r) {console.log(r); },
error: function(r){console.log(r); },
url: 'http://localhost:8080/data/api/v1/entry/',
cache:false
});
});
});
我问this质询时,因为我认为这会有所帮助,但它不是。它有更多的上下文,即关于我用于身份验证的内容,但如果需要,我可以提供更多的细节。
刚作为猜测,REST端点可能没有设置为允许POST请求。我不知道它如何与TasteyPie一起工作,但这就是它与DRF一起工作的方式。 – OozeMeister