我想追加一個json對象到頁面。我通過ajax獲得成功,但沒有顯示出來。Ajax json響應沒有附加到頁面
<form onsubmit="return false;">
{% csrf_token %}
Search:<input type="text" name="artist" id="artist" />
<button class="updateButton" onclick="createlist()">submit</button>
</form>
function createlist(){
$.ajax({
type: "POST",
url: "/rest/",
dataType: "json",
data: {
artist: $('#artist').val()
},
headers: {
'X-CSRFTOKEN': "{{ csrf_token }}",
},
success: function(data){
$('#output').html(data); //also tried data.content
}
});
}
當我在devtools中查看響應和預覽時,響應似乎位於一個長轉義字符串中。
"[{\"fields\": {\"artist\": \"Leonardo da Vinci\", \"link\": \"https://trove2.storage.googleapis.com/leonardo-da-vinci/galloping-rider-and-other-figures.jpg\", \"title\": \"Galloping Rider and other figures\"}, \"model\": \"serve.art\", \"pk\": 63057}
我該如何將它轉換成可用的東西?
仍然不是數據附加到頁面,當我做到這一點。 '$('#output')。html(JSON.parse(data));'不顯示。 – 2014-12-04 03:38:08
抱歉可能會誤解您的問題。設置一個斷點,看看$('#output')是否得到元素 – Ryo 2014-12-04 03:48:29
它現在正在工作,但我什麼也沒有改變,我所做的只是設置了一個斷點並逐步完成了devtools中的函數。這很奇怪,我想知道是什麼修復了它。 – 2014-12-04 04:04:49