0

I'm try to get data from php web-service through the java-script.In my project error occurred when parse the JSON.

My PHP web service like this

<?php
header("Content-Type:application/json");
try {
 if(!empty($_POST['mobile_number']))
 {
 $number = $_POST['mobile_number'];
 $number = $_POST['user_type'];
 deliver_response(200, "success", $number); 
 }
 else 
 {
 deliver_response(400, "no parameter", $number);
 }
} catch (Exception $e) {
 echo("message".$e->getMessage());
}
function deliver_response($status,$status_message,$data)
{
 header("HTTP/1.1 $status $status_message");
 $response['status'] = $status;
 $response['status_message'] = $status_message;
 //$response['data'] = $data;
 $json_response = json_encode($response);
 echo $json_response; 
}
?>

And Im try to call above webservice like this

function logingAuth(){
 var mnumber= $("#uname").val();
 var utype= $("#pword").val();
 try{
 $.post("http://192.168.1.55/rest/signup.php", { mobile_number: mnumber,user_type:utype }, parseResults);
}
catch(err)
{
alert(err.message);
}
 function parseResults(json){
 var obj = jQuery.parseJSON(json);
 alert(obj.status);
 } 
}

but this one not working because of var obj = jQuery.parseJSON(json); line.

If I call like this Its working

function parseResults(json){
 alert(json.status);
 } 

What is this problem?

asked Jan 21, 2015 at 9:10
1
  • when u parseJSON, you are decode it into array. then you cant do the json.status Commented Jan 21, 2015 at 9:14

1 Answer 1

4

jQuery will automatically recognise the response type for you and act appropriately. In this case it will automatically deserialise the JSON to an object, so your call to parseJSON will cause an error as you are trying to deserialise an object. Simply remove that line:

function parseResults(json){
 alert(json.status);
} 
answered Jan 21, 2015 at 9:12
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your quick reply.Actually I use jQuery mobile.is that oky to implement without that line.
Thats fine, this is part of plain jQuery. jQM has no effect on this.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.