【问题标题】:json request and django responsejson 请求和 django 响应
【发布时间】:2011-06-18 09:19:04
【问题描述】:

为什么我在发送 json 请求后没有得到服务器的响应,我做错了什么

  $("#chat").submit(function(){
            // If user clicks to send a message on a empty message box, then don't do anything.
            alert($("#msg").val());
            alert(url);
            if($("#msg").val() == "") return false;


            $.post(url,
                             alert('22'),

                            {
                            time: timestamp,
                            action: "postmsg",
                            message: $("#msg").val()
                    },
                    function(load) {
                                                    $("#msg").val(""); // clean out contents of input field.
                                                    // Calls to the server always return the latest messages, so display them.
                                                    processResponse(payload);
                                                    },
                    'json'
    );

Django 视图函数:

   def ajaxcall(request): 
     #I have tried both the return statements and there are no exceptions since see the logging
     #logging.debug(response) 
     #return HttpResponse(simplejson.dumps(response), mimetype='application/javascript')
     #return response

【问题讨论】:

    标签: javascript django json django-models django-views


    【解决方案1】:

    我不确定您的 jQuery POST 语法是否正确。

    这是一个例子:

    $.post(
        '/url/to/post', // url to submit to
        $("#myform").serialize(), // be sure to serialize form before submitting
        function(data) { // run after form submit
            doStuff();
    },
    'json'
    );
    

    记住这里的顺序很重要。第二个参数应该是数据,但您的第二个参数似乎是对警报的调用。

    如果您要提交表单或表单中的某些内容,则需要序列化表单元素。

    为了更好地控制这个过程,您可能想尝试在 jQuery 中使用较低级别的 $.ajax() 函数。

    【讨论】:

      猜你喜欢
      • 2015-06-20
      • 2014-11-26
      • 2011-10-23
      • 2013-03-30
      • 1970-01-01
      • 2011-02-18
      • 2011-10-31
      • 2016-05-01
      • 1970-01-01
      相关资源
      最近更新 更多