My json data won't appear in browser. It's my first time using json and I can't figure out the problem.I searched on internet and it was related with mime but still can't figure it out. This is the code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var jsonData = '[{"rank":"9","content":"Alon","UID":"5"},{"rank":"6","content":"Tala","UID":"6"}]';
$.ajax({
url: '/echo/json/',
type: 'POST',
contentType:"application/json; charset=utf-8",
data: {
json: jsonData
},
success: function (response) {
var trHTML = '';
$.each(response, function (i, item) {
trHTML += '<tr><td>' + item.rank + '</td><td>' + item.content + '</td><td>' + item.UID + '</td></tr>';
});
$('#records_table').append(trHTML);
}
});
</script>
</head>
<body>
<table id="records_table" border='1'>
<tr>
<th>Rank</th>
<th>Content</th>
<th>UID</th>
</tr>
</table>
</body>
</html>
Here is the updated code
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var jsonData = '[{"rank":"9","content":"Alon","UID":"5"},{"rank":"6","content":"Tala","UID":"6"}]';
$(document).ready(function(){
$.ajax({
url: '/echo/json/',
type: 'POST',
contentType:"application/json; charset=utf-8",
data: {
json: jsonData
},
success: function (response) {
var trHTML = '';
$.each(response, function (i, item) {
trHTML += '<tr><td>' + item.rank + '</td><td>' + item.content + '</td><td>' + item.UID + '</td></tr>';
});
$('#records_table').append(trHTML);
}
});
});
</script>
</head>
<body>
<table id="records_table" border='1'>
<tr>
<th>Rank</th>
<th>Content</th>
<th>UID</th>
</tr>
</table>
</body>
</html>
You were missing the document ready block around your ajax call.
Write a Error method and see if some ajax error has occured
`$.ajax({
success: function(){
// Handle the success event
},
error: function(xhr){
// Handle the error event
}
// ......
});`
The xhr will have response text
UPDATE:
your code is perfectly fine as nikhil said dodument.ready should do the magic
check the fiddle i have tested with your code http://jsfiddle.net/8sfm0p14/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.