-3

I saw a similar discussion here. But my returned data format is a bit different. I have a Json string returned from server as shown below

<!DOCTYPE HTML>
<html>
<head>
<style>


</style>
</head>
<body>

{"error":false}</body>
</html>

I need to extract data "error":false from the Json string. I tried as below

(1)Say the Json message is in result variable, then I checked as

if(result.error == false)

It doesn't work.

(2) I also used jQuery.parseJSON as discussed in this link. It doesn't work also. How can I parse Json data?

Json data is parsed in Jquery.ajax for the returned message as follow

jQuery.ajax({
        url: "registrationdatavalidationatserver.php",
        type: "POST",
        data: $("form").serialize(), /*grab all elements in your form and serialize them */
        success:  function(result){
           //To parse result

        },
        error: function(){
           // the AJAX request failed as in timed out / bad url etc.

        } 
   });

(3) How to return message from server just Json data without

<html>, <head>, <style>, etc. tags?

I returned from server as echo json_encode($data);

Community
  • 1
  • 1
batuman
  • 7,066
  • 26
  • 107
  • 229

1 Answers1

2

I have a Json string returned from server as shown below.

No, you need to have only the following returned from the server:

{"error": false}

Say the Json message is in result variable, then I checked as if(result.error == false)

It will never work because it is a HTML. Not a JSON.

How to return message from server just Json data without <html>, <head>

Even before you send the output, please make sure you are not sending anything to browser. Have these defined in the PHP headers:

<?php
  header("Content-type: application/json");
  die(json_encode($arrayData));
?>

There must not be anything other than this. It is wise to have a dedicated file for JSON output too, if you are confused in making this kind of setup.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252