0

Here is what I have in PHP:

$arrayresults = array();
while($popularbrushesrow = mysql_fetch_array($popularBrushes)){

$arrayresults[]  = '<a href="brushdescription.php?id='.$popularbrushesrow['bd_brushid'].'"><img class="slideImg" alt="'.$popularbrushesrow['bd_brushname'].'" title="'. $popularbrushesrow['bd_brushname'].'" src="'.$popularbrushesrow['bd_imagefilepath'].'"  /></a>';

}

echo json_encode($arrayresults);

Now, jquery:

$.ajax({
    type:'GET',
    url:'getDataForSlide.php',
    data:"limit="+limit+"&required="+required,
    dataType:"json",
    cache:true,
        success: function(result){
     var arrayFromPHP = JSON.parse(result);
     alert(arrayFromPHP);
  }
})

Could someone please help me out. Whats the correct way to form an array in JSON?

4
  • Are you actually returning anything? is that data actually appending to the GET? Commented Jun 18, 2013 at 21:10
  • 2
    If you specify the datatype, jQuery will already parse the JSON for you. Commented Jun 18, 2013 at 21:10
  • 2
    do a console.log(result) and see what comes through. Probably you've got a shutdown script or something outputting non-json data AFTER your json_encode call, leading to the error. e.g. an html footer or something. Commented Jun 18, 2013 at 21:10
  • what do you get when you consol.log(result); Commented Jun 18, 2013 at 21:10

3 Answers 3

1

The problem is likely to be this line:

var arrayFromPHP = JSON.parse(result);

Because you've specified dataType: 'json' in the ajax options, jQuery has already done the parsing for you. So doing it a second time starts out by doing toString on the array, which does a join, which results in invalid JSON.

Simply use result directly.

For example, suppose you have this JSON:

[
    "<a href=\"http://stackoverflow.com\">Stack Overflow</a>",
    "<a href=\"http://google.com\">Google</a>"
]

Because jQuery has already done JSON.parse on it, result is an actual array. So if you then pass it into JSON.parse, the first thing that does is toString, which gives you this:

<a href="http://stackoverflow.com">Stack Overflow</a>,<a href="http://google.com">Google</a>

...which is not, of course, valid JSON.

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

Comments

0

I would simplify your jquery....like this...

$.getJSON("getDataForSlide.php", { limit: limit, required: required}, function(json) {
console.log(json);
});

Comments

0

I like to use

jQuery.parseJSON(response);

And don't forget to use die(); or exit(); on php side after you echo your results, since it's a ajax call. This information can be found here: http://codex.wordpress.org/AJAX_in_Plugins

1 Comment

I didnt mark it..... :) but it could be because he is using json as a datatype, so the parse will already be handled for him

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.