1

I need to return an array in php and use it in javascript. When I print the returned value it prints empty string. How do I make mistake?

$.ajax({
     type: "POST",
     url: "sort2.php",    
     success: function(result){      

     alert(result[0]);                                            

    }
});

//sort2.php

$data[0] = "book";
$data[1] = "pen";
$data[2] = "school";
echo json_encode($data);
2
  • what happens when you alert(result) Commented Apr 9, 2012 at 7:18
  • it gives an empty string Commented Apr 9, 2012 at 7:19

4 Answers 4

2

You can change the dataType of your $.ajax() request to json and have jQuery automatically decode your JSON object from string.

Alternatively, you could add to your PHP header('Content-Type: application/json') and jQuery should also automatically decode your JSON string.

Sign up to request clarification or add additional context in comments.

6 Comments

with no dataType set, it should default to auto. Is the particular formatting being returned such that it won't auto detect the JSON?
@GregPettit I believe auto looks at the MIME type to try and guess the format. If the response's MIME is text/html (PHP's default MIME type response), it is going to guess wrong.
I've always explicitly set dataType myself (it makes for better readability, too), but I was curious. Certainly it wouldn't hurt to be explicit!
I have written as dataType: 'json', success: function(result){ alert(result[0]);... but doesnt work
@user1077300 doesn't work doesn't help anyone. Did you get an error? What was produced? What does result look like?
|
1

You have to parse the JSON data

$.ajax({
     type: "POST",
     url: "sort2.php",    
     success: function(result){      

     //Parse Json to Array 
     result = jQuery.parseJSON(result);
     alert(result) // would output the result array                                      

    }
});

1 Comment

Not necessary; see alex's answer.
1

In your sort2.php file, change the following code:

echo json_encode($data);

to

header('Content-Type: application/json');
echo json_encode($data);

What it does is tell the client browser to treat the response as JSON data and parse accordingly and not as HTML, which is the default.

Also, make sure sort2.php file is in the same folder as the html file from where you are making the ajax call.

Hope this helps.

Comments

1

you also can use the $.post method by jQuery, which would look like this: (so you don't need to change your php script, see here: jQuery Post).

$.post(
    'sort2.php', //script you request
    {}, //post-data you want to send
    function(result) {
        alert(result[0]); /*i like more: console.log(result); this way you can see it in firebug e.g.) */
    },
    'json' //data-type you expect
);

6 Comments

when I run this code in server it works. but in my local it gives en error in the alert(result[0]) line as it is null, or doesnt an object. what can be the problem?
try 'alert(result);' or better with 'console.log(result);' (which will be displayed in firebug->console). And check with firebug what the php script returns in firebug (might be the problem of local php). Php error won't be displayed when using ajax everytime. link
alert(result); gives "undefined", when I write console.log it says console "undefined"
then there is a problem with your php. Need to know what your phpscript returns
when I run php script, it prints nothing. json_encode doesnt work in my local then.
|

Your Answer

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.