試圖做一個帖子在阿賈克斯與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