Reputation: 452
I am using the jquery function $.getJson. It is sending the data I want and the PHP script producing the JSON is working as it should. However, the problem I am having occurs from here.
In my $.getJSON code, I want it to simply log the results if successful and log as an error if an error... You guessed it... I'm getting an error...
Here is my JSON code being passed to it that is producing the error but I can not see what is wrong with it....
{
"results":{
"city":[
"metric":"visits",
"dimension":"city",
"data":[["London","5"],["Glasgow","3"],["(not set)","1"],["Aberdeen","1"],["Antalya","1"]]
],
"browser":[
"metric":"visits",
"dimension":"browser",
"data":[["Internet Explorer","11"],["Chrome","9"],["Firefox","3"],["Safari","3"],["Android Browser","2"]]
],
"screenResolution":[
"metric":"visits",
"dimension":"screenResolution",
"data":[["1280x800","5"],["1680x1050","4"],["1024x768","3"],["1366x768","3"],["1280x1024","2"]]
]
}
}
UPDATED TO SHOW CODE
This is my code used for getting the results
$.ajax({
//DEFINE URL being called by Ajax
url:'./getAnalytics.php',
dataType: "json",
data:{dateToSearch:dateToSearch},
success:function(resultsToView){
console.log(resultsToView);
},error:function(resultsToView){
console.log("Error: " + resultsToView);
}
});
And the console shows:
When I get this back, the error function is called rather than success. Please help.
Upvotes: 0
Views: 90
Reputation: 665536
Your JSON is invalid. The square brackets denote an array (list), where the items do not have keys. Change
[
"metric":"visits",
"dimension":"…",
…
]
to object (map) syntax with curly braces:
{
"metric":"visits",
"dimension":"…",
…
}
Also, the error callback has the signature Function(jqXHR xhr, String textStatus, String errorThrown)
. You're logging the stringification of the xhr
object, which is not helpful. Better use
…,
error: function(xhr, status, error) {
console.log("Error: "+error+" ("+status+")", xhr);
}
Upvotes: 3