[英]How does one utilize JSON response data from an AJAX call?
我似乎無法從AJAX調用執行任何代碼,也無法利用JSON響應。 我想念什么? 調用成功,並且在Firebug中可以看到腳本在服務器端可以很好地執行,但是我似乎無法對返回的數據進行任何處理。
review.single = {
id: null,
ip: null,
el: null,
addFlag: function(){
if(this.id && foo.user_id){
$.ajax({
type: 'post',
url: '/reviews/add_flag/',
data: ,
dataType: 'json',
success: function(data){
if(data.success){
this.el.text('Flagged');
this.el.css({
'text-indent': none,
backgroundColor: '#c00',
color: '#fff'
})
} else {
$('#growl').text(data.message);
$('#growl').fadeIn(500).delay(5000).fadeOut(200);
}
}
})
}
}
}
我在一種呼叫響應類型上得到了回復:
{success: false, message: "You have already flagged this item."}
理想情況下,我希望能夠接收消息並將其放入咆哮中並顯示出來,並使用success
屬性作為檢查。
更新:
我添加了一個error
塊; 看起來像這樣:
error: function (request, status, error) {
alert(request.responseText);
}
結果,我收到一條警告:
{success: false, message: "You have already flagged this item."}
弄清楚了。
我的JSON顯然格式不正確,並且屬性需要用"
包裹"
。這將錯誤返回給jQuery。感謝您的幫助。
例:
{"success": false, "message": "You have already flagged this item."}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.