![](/img/trans.png)
[英]Alternative render of JSON response in Django depending on request origination
[英]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.