2

In my php file I have:

$arr[] = array("status"=>0);
echo json_encode($arr);

In my javascript, I have:

$(document).ready(function(){

    initialize(); 
    $("#searchbutton").click(function(){

    var usrinput = $("#userinput").val();

    $.get(
        "searchpageajaxjson.php",
        {searchterm: usrinput},
        function(data){
            searchreturned(data);
        },
        "json"
    ); 
    });
 });


function searchreturned(data){
   console.log(data); 
   var parsed = jQuery.parseJSON(data);
   console.log(parsed); 
   //for (var i = 0; i < parsed.length; i++) {  
    //            alert(parsed[i]);  
    //     } 
   //    
}

The console.log(data) shows [object Obejct] and console.log(parsed) shows null

Where am I going wrong?

Edited to Add:

alert(data.status) shows undefined.

Second Edit:

I'm really grateful for all the help I've received. It's hard to pick an answer, because I'm really only able to move on from this problem due to all the help received in all the comments. I am always in awe of you kind folks who give your time to helping newbies like myself.

11
  • Can you please share the json as text and the [Object object]? Commented Jul 22, 2012 at 23:39
  • 3
    What console are you using? It should show the object properties. Commented Jul 22, 2012 at 23:39
  • You jumped from using $. as the jQuery identifier and then to jQuery. - any reason? $.parseJSON would have worked too... Commented Jul 22, 2012 at 23:42
  • @Esailija. This is the first time I am doing this - I opened up the developer tools on the browser (IE) and there is a console tab which shows the following: LOG: [object Object] LOG: null Commented Jul 22, 2012 at 23:42
  • @OscarJara, I'm not sure what you mean Commented Jul 22, 2012 at 23:43

2 Answers 2

2

You're not supposed to call jQuery.parseJSON on data again. jQuery takes care of that for you already, since you used "json" as the dataType.

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

7 Comments

Would it make a difference if I said even before attemping to use parse.JSON, alert(data.status) is saying undefined?
@ValerieParker: Yes, it would. Make sure the PHP outputs no warnings or other cruft apart from the JSON.
That echo json_encode($arr) statement is the only echo statement. I do have this sort of thing as well: if (!$connection){ die('Not connected: '.mysql_error()); }
@ValerieParker: Yes, it would, if there was an error. Or, if you have warnings and such turned on, other problems could be causing it too. Which browser are you using?
@ValerieParker: Also, alert(data.status) isn't the right one. Try alert(data[0].status) instead.
|
1

1. There is no reason to call jQuery.parseJSON or $.parseJSON because jQuery knows that is already a JSON object due to the "json" parameter you passed here:

$.get('source', {param:param}, function(data){ /*work with data*/ }, "json");

2. If console.log(data); is not null and you are getting some [Object Object] try to see what are the properties of that object, like this:

e.g: Explore your object:

enter image description here

e.g: Print value from object:

//this will output "Horror"
data.genre

Hope this helps you.

1 Comment

Oscar Jara, thanks, I now understand what you mean. And when I used the Chrome console instead, I can see the values.

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.