2014-02-21 109 views
0

我无法成功打印json对象。但服务器获取请求。它也可以提供回应。我用POSTER测试了这项服务。请让我知道,问题在哪里?无法成功获取json对象

<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script type="text/javascript"> 
function loginCheck() { 
    alert("inside function"); 
    $.ajax({ 
    url: "http://localhost:61852/myapp/loginCheck", 
    type: "POST", 
    data: "username=username&password=password", 
    success: function (data) { 
      alert(data); 
     } 
    }); 
    } 
</script> 
</head> 
<body> 
    <input type="submit" value="submit" onClick="loginCheck();"> 
</body> 
</html> 
+0

您的成功处理程序是否被调用? alert()显示什么?为了知道发生了什么,你必须给我们一些基本的调试信息。 – jfriend00

回答

0

不要把它看作一个解决方案,但由于一些提示搜索你的... 如果你给后面的输出我会可以帮你多一些。

要看到控制台(在Chrome)
WIN:

Ctrl + Shift + J 

OSX:

Cmd + Opt + J 


function loginCheck() { 
    // alert("inside function"); 
    $.ajax({ 
    url: "http://localhost:61852/myapp/loginCheck", 
    type: "POST", 
    data: "username=username&password=password", 
    success: function (data) { 
      console.log("data: "+data); 
      console.dir(JSON.parse(data)); 
     } 
    }); 
    } 
1
 
1. add this dataType :'json' to your ajax parameter, 
2. make sure server side response json type,you can force server side response 
    json type in php give a header: 

    header('Content-Type: application/json; charset=utf-8'); 
    echo jsonencode($datasArray)) 

1

尝试将dd dataType: "json",看。

$.ajax({ 
    type: 'POST', 
    url: "url", 
    data: "qs", 
    dataType: "json", 
    success: function (resultData) { 
     // 
    } 
});