【问题标题】:Django + JQuery Ajax postDjango + JQuery Ajax 帖子
【发布时间】:2011-12-21 22:17:36
【问题描述】:

我已经阅读了有关主题的所有内容,但我不明白哪里出了问题。

$(document).ready(function() {
    $.post("tweet.py", {
        lat : searchResult._point.__lat,
        lng : searchResult._point.__lng,
        text : searchResult.text
    },  function(data) {
           alert(data);
        });
    });

处理程序的代码:

import django

def iEventAjax(request):
if request.is_ajax():
    return HttpResponse("ok")

警告信息是

"import django
def iEventAjax(request):
    if request.is_ajax():
        return HttpResponse("ok")
" 

而不是“好的”

怎么了?

【问题讨论】:

  • 当然,您必须发布到服务器上的 URL,而不是文件。
  • 我应该写什么? $.post("/", { ?
  • 哪个网址?我在项目目录中有两个文件,一个包含 js 和 html 的文件,我从中发送帖子和文件 tweet.py
  • 你大概知道如何在 Django 中编写 URLconfs。该 URLconf 中的 URL 之一(也可能)连接到您的“处理程序”功能。这就是您要发布到的 URL。

标签: jquery python ajax django post


【解决方案1】:

请在您的 urls.py 中添加一个类似

的网址
(r'^ajaxrequest$', 'app.views.iEventAjax')

然后改变

$(document).ready(function() {
    $.post("/ajaxrequest", {
        lat : searchResult._point.__lat,
        lng : searchResult._point.__lng,
        text : searchResult.text
    },  function(data) {
           alert(data);
        });
    });

在您应用的views.py中

def iEventAjax(request):
    return HttpResponse("ok")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 2010-11-12
    • 2012-06-14
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多