2011-01-31 147 views
0

爲什麼不我從服務器的響應發送json的請求後,我是什麼doinfg錯JSON請求和響應的Django

$("#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 

回答

0

我不是確定你的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()函數。