0

I really need help on this one. I've been looking at it for hours and been getting nowehere. I am working with ajax in WordPress and this is kind of my first time doing so.

I have a function in my code that utlimately ends with the following:

echo json_encode($aData); 

I then have code in my js file written as follows:

 jQuery.ajax({
    url: "path/to/admin-ajax.php", //AJAX file 
    // dataType: 'json',
    type: 'POST',
    // contentType: 'application/json',     
    data: { action: "process_chart_data" },             
    success: function(chartData) {
        //Code, that need to be executed when data arrives after
        // successful AJAX request execution
        console.log("SUCCESS");         
        console.log(chartData);  

    }
});

You will notice the comments in the above code. When I view chartData in the console I get this:

{"title":"R=3+","dates":["06\/04\/2014","11\/05\/2014","15\/06\/2014","27\/07\/2014","17\/08\/2014","28\/09\/2014","05\/10\/2014","19\/10\/2014","09\/11\/2014","23\/11\/2014","14\/12\/2014"],"series":[{"data":[[1396738800000,284],[1399762800000,350],[1402786800000,212],[1406415600000,296],[1408230000000,220],[1411858800000,253],[1412463600000,200],[1413673200000,310],[1415491200000,180],[1416700800000,156],[1418515200000,290]],"name":"Bulls"}]}0

However when I uncomment the dataType and contentType this outputs a 0.

My issue is I am trying to access properties of the returned array such as chartData.title but all I get is 'undefined'.

I have looked at this from a few different approaches but after a few hours I have had to throw my hands up. I would be greatly appreciative for any assistance people might be able to provide here.

1 Answer 1

1

I am not sure the output you have provided is the correct one but do you notice the '0' character at the end of your string?

{"title":"R=3+","dates":["06\/04\/2014","11\/05\/2014","15\/06\/2014","27\/07\/2014","17\/08\/2014","28\/09\/2014","05\/10\/2014","19\/10\/2014","09\/11\/2014","23\/11\/2014","14\/12\/2014"],"series":[{"data":[[1396738800000,284],[1399762800000,350],[1402786800000,212],[1406415600000,296],[1408230000000,220],[1411858800000,253],[1412463600000,200],[1413673200000,310],[1415491200000,180],[1416700800000,156],[1418515200000,290]],"name":"Bulls"}]}0

That is enough to cause the JSON parser to fail.

Also, don't use the contentType property like that since you don't send any JSON, you should remove that line altogether.

3
  • Yes, there is a zero at the end there. Is that me adding that somehow? I don't send any JSON to the request but that is what is coming back. So in that instance is it still correct to drop the contentType property? Commented May 12, 2015 at 15:49
  • 1
    You should call the exit function (technically a language construct but never mind that) right after you output the response. The contentType property is totally unnecessary to be specified since, just like the documentation that I have linked to states, you should alter that property only when you are sending data in the JSON format. The dataType is the one you need to let know jQuery how to handle the response NOT the contentType.
    – webtoure
    Commented May 12, 2015 at 15:57
  • Thanks. I was unhappy getting my code to work without really understanding what was going on. You've been a big help in that regard. Commented May 12, 2015 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.