无法获得成功的json对象

时间:2014-02-21 07:55:15

标签: javascript jquery ajax json

我无法在成功时打印json对象。但服务器获取请求。它也可以给出响应。我用POSTER测试了这项服务。请告诉我,问题出在哪里?

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

3 个答案:

答案 0 :(得分: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 :(得分:1)

尝试添加dataType: "json",然后看。

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

答案 2 :(得分:0)

不要把它作为解决方案,但作为搜索你的一些提示...... 如果你回馈输出,我会帮你一些。

查看控制台(在chrome上)
赢了:

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));
        }
   });
  }