【问题标题】:Not working jquery Post request不工作的jQuery发布请求
【发布时间】:2016-04-09 16:16:24
【问题描述】:

我正在 django 上写页面。不工作 jquery Post 请求。单击#encrypt 后没有任何反应。我尝试通过 $.get、$.ajax 提出请求 - 没有。在$.post之前一切正常

home.html:

$(document).ready(function() {

    $("#encrypt").click(function () {
        var postData = {
            text: $("#input-box").val(),
            rotate: $("#rotate").val()
        };
        $.post('encrypt', postData, function (data){
            alert(data);
        });
    });
});

views.py:

def home(request):
    return render_to_response("home.html", {})

def encrypt(request):
    text=request.POST["text"]
    #some manipulation with text
    return render_to_response("home.html", {'text': text})

urls.py:

urlpatterns = [
    url(r'^$', caesar.views.home, name="home"),
    url(r'^encrypt$', caesar.views.encrypt, name="encrypt")
]

【问题讨论】:

  • 您能否用您已经尝试过的一些方法更新您的问题?如果您向事件处理程序添加断点,您是否看到它被命中/它到达了多远?
  • 看起来有语法错误。 “函数数据{”应该是:“函数(数据){”。
  • 只是文本错误。在我的代码中不是
  • 嗯,您的网址好像有误。尝试将 $.post('encrypt', 更改为 $.post('/encrypt',
  • 现在可能有效,但是 alert(data);显示此页面的 html 代码。有什么问题?

标签: jquery python ajax django


【解决方案1】:

您对$.post() 的调用在点击函数处理程序之外。删除前面多余的});

适当的缩进可能会让您自己发现。

【讨论】:

    猜你喜欢
    • 2017-08-20
    • 2020-08-14
    • 2013-08-04
    • 2018-01-08
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    相关资源
    最近更新 更多