我想追加一个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