为什么我的json数据在运行代码时不会出现在浏览器中

时间:2015-06-06 19:40:54

标签: javascript jquery html ajax json

我的json数据不会出现在浏览器中。这是我第一次使用json,我无法弄清楚问题。我在互联网上搜索它与mime有关,但仍然无法弄明白。这是代码:

<!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>

2 个答案:

答案 0 :(得分:0)

这是更新后的代码

'Place this code in a Module

Private Declare Function FindWindowA Lib "USER32" _ 
(ByVal lpClassName As String, _ 
ByVal lpWindowName As String) As Long 

Private Declare Function GetWindowLongA Lib "USER32" _ 
(ByVal hWnd As Long, _ 
ByVal nIndex As Long) As Long 

Private Declare Function SetWindowLongA Lib "USER32" _ 
(ByVal hWnd As Long, _ 
ByVal nIndex As Long, _ 
ByVal dwNewLong As Long) As Long 

Option Explicit 

Sub FormatUserForm(UserFormCaption As String) 
    Dim hWnd            As Long 
    Dim exLong          As Long 
    hWnd = FindWindowA(vbNullString, UserFormCaption) 
    exLong = GetWindowLongA(hWnd, -16) 
    If (exLong And &H20000) = 0 Then 
        SetWindowLongA hWnd, -16, exLong Or &H20000 
    Else 
    End If 
End Sub 
Sub ShowForm() 
    UserForm1.Show 
End Sub 
 'Place this code in a UserForm with one Command Button named CommandButton1.
Option Explicit 

Private Sub CommandButton1_Click() 
    Unload Me 
End Sub 

Private Sub UserForm_Initialize() 
    Call FormatUserForm(Me.Caption) 
End Sub 

您错过了围绕ajax呼叫的文档就绪模块。

答案 1 :(得分:0)

编写一个Error方法,看看是否发生了一些ajax错误

`$.ajax({
   success: function(){
     // Handle the success event
   },
   error: function(xhr){
     // Handle the error event
   }
   // ......
 });`

xhr将有响应文本

更新:

你的代码非常好,因为nikhil说dodument.ready应该做的魔术

检查我使用您的代码http://jsfiddle.net/8sfm0p14/

测试过的小提琴