Ajax 调用 Django(Piston) API 总是失败

发布于 2024-11-15 06:13:57 字数 806 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

儭儭莪哋寶赑 2024-11-22 06:13:57

在 django 1.2.5 和 1.3 中,Ajax 表单提交需要 csrf 令牌。

In django 1.2.5 and 1.3, Ajax form submits expect a csrf token.

无悔心 2024-11-22 06:13:57

您确定这是正确的网址吗?它有一个非常奇怪的结构。我希望看起来像 GET 参数的元素实际上是 GET 参数:

{{SITE_URL}}requests/api/manage/?disc={{vialogue.discussion_id}}&puser={{req.userid}}&acc=0

如果您进行更改,它会起作用吗?

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:

{{SITE_URL}}requests/api/manage/?disc={{vialogue.discussion_id}}&puser={{req.userid}}&acc=0

Does it work if you make that change?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文