Roxx
Roxx

Reputation: 3986

JSON decode is not working with PHP

I have a WAMP server with PHP 5.4.3 version. I encoded the data in JSON by php file below is the code (submit.php).

$username = 'User';
$comment = 'Test comment';
$date = date("Y-m-d G:i:s");  
$image = '/img/sephiroth.png';

  $return = array(
  'username'=> $username,
  'comment' => $comment,
  'date' => $date,
  'image' => $image  );
  echo json_encode($return); 

Now i have a javascript file(script.js).

$.ajax({
            type: "POST",
            url: "submit.php",
            data: dataString,
            cache: false,
            success: function(result){
                    alert(result); //It is showing all the JSON data.
                    $value = result;
                    $data = json_decode($value,true);
                    alert($data);
                        }
           });

I want to decode all the JSON data separately. e.g Username, comment, date, image. So, i can show them on webpage, at the moment all data is coming together. I tried multiple times with multiple options (php array or result.username or result['username'] but no luck.

Now i am getting below error.

ReferenceError: json_decode is not defined
    $data = json_decode($value,true);

Upvotes: 1

Views: 12915

Answers (5)

Shiji.J
Shiji.J

Reputation: 1631

My example:

Server-side returns {"username":"xxxxx","data":"xxxxxxxxxxxx and more"}

You can just deal with json like a javascript array, do not need to decode it. the data below itself is the array of result.

 $.ajax({
        type: "POST",
        url: "submit.php",
        cache: false,
        success: function(data){
           var username = data['username'];
           var data = data['data'];
           //and so on
        }
       });

And make sure php sent the header Content-Type:application/json

header('Content-Type: application/json');

Upvotes: 0

Jagadeesh
Jagadeesh

Reputation: 754

try like below:

$.ajax({
        type: "POST",
        url: "submit.php",
        data: dataString,
        cache: false,
        success: function(result){
                alert(result); //It is showing all the JSON data.
                jQuery.parseJSON( result );
                    }
       });

Upvotes: 1

Ashwani Goyal
Ashwani Goyal

Reputation: 616

Try the following:

success: function(result){
         var res = $.parseJSON(result);
         var data = [res.username, res.comment, res.date, res.image]; // Array of the received response
}

This way you can get required output

Upvotes: 2

akshik
akshik

Reputation: 26

Use the dataType option of ajax and set its value to json. Then you can access the response as result['username']

Upvotes: 0

Noman
Noman

Reputation: 4116

you cannot use json_decode in JS json_decode is PHP function

use jQuery.parseJSON(json_data) to parse your json data;

jQuery.parseJSON( result );

Change

$value = result;
$data = json_decode($value,true);

To :

jQuery.parseJSON( result );

http://api.jquery.com/jQuery.parseJSON/

Upvotes: 4

Related Questions