繁体   English   中英

json请求和Django响应

[英]json request and django response

发送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

我不确定您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'
);

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM