Ajax 调用 Django(Piston) API 总是失败
我正在使用 ajax 调用使用 Piston/Django 创建的 API。我通过直接在浏览器中输入 API URL 来测试它们是否正确。
然而,ajax请求总是会触发错误回调函数,但却返回未定义的错误。我认为问题出在我的 ajax 调用中。有人可以帮助我吗?多谢。
这是我的 JavaScript:
$("#delete_req").click(function(event){
//PUTs data, saving new permissions
alert("delete_req");
event.preventDefault();
$.ajax({
url:"{{SITE_URL}}requests/api/manage/disc={{vialogue.discussion_id}}&puser={{req.userid}}&acc=0/",
type:'GET',
success: function(data, textStatus, jqXHR){
location.reload( true );
},
error: function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
alert(textStatus);
alert("There was an error deleting this request. Please try again or contact us for help.")
}
});
});
I'm using an ajax call to my API created with Piston/Django. I tested that the API URLs are correct by directly typing them in the browser.
However, the ajax request always triggers the error callback function but returns an undefined error. I think the problem is somewhere inside my ajax call. Could anyone help me? Thanks a lot.
Here is my javascript:
$("#delete_req").click(function(event){
//PUTs data, saving new permissions
alert("delete_req");
event.preventDefault();
$.ajax({
url:"{{SITE_URL}}requests/api/manage/disc={{vialogue.discussion_id}}&puser={{req.userid}}&acc=0/",
type:'GET',
success: function(data, textStatus, jqXHR){
location.reload( true );
},
error: function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
alert(textStatus);
alert("There was an error deleting this request. Please try again or contact us for help.")
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 django 1.2.5 和 1.3 中,Ajax 表单提交需要 csrf 令牌。
In django 1.2.5 and 1.3, Ajax form submits expect a csrf token.
您确定这是正确的网址吗?它有一个非常奇怪的结构。我希望看起来像 GET 参数的元素实际上是 GET 参数:
如果您进行更改,它会起作用吗?
Are you sure that's the correct URL? It has a very strange structure. I would expect the elements that look like GET parameters to actually be GET parameters:
Does it work if you make that change?