Butchi Reddy Velagala
Butchi Reddy Velagala

Reputation: 864

unable to get the json object on success

I am unable to print the json object on success. But the server gets the request. And it can give the response also. I tested the service with POSTER. Please let me know, where the problem is?

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

Upvotes: 0

Views: 142

Answers (3)

yeyene
yeyene

Reputation: 7380

Try to add dataType: "json", and see.

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

Upvotes: 1

Raymond Cheng
Raymond Cheng

Reputation: 2505

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

Upvotes: 1

Gromish
Gromish

Reputation: 199

Don't take that as a solution, but as some hints to search yours... If you give back the output I'll can help you some more.

To see the console (on 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));
        }
   });
  }

Upvotes: 0

Related Questions