I have a php application that makes an ajax call and retrieves the following data from my database:
{
"5717a3873747106e60a087ec": {
"_id": {
"$id": "5717a3873747106e60a087ec"
},
"phone": "18455100020",
"fax": false,
"emergency": false,
"site": "ABC",
"sip_uri": "",
"associated_users": [],
"location": "New Zealand",
"reservation": [{
"reserved": true,
"reserved_for": "COO12"
}],
"available": true
}
}
This is the code that I have to make the ajax call:
$(document).ready(function(){
var request = BASEPATH + 'index.php/export/get_widget/available';
console.log(request);
$.ajax({
url:request,
type:'GET',
dataType:'jsonp',
success: function(returnDataFromController) {
if (returnDataFromController.status)
{
console.log(returnDataFromController);
//build table contents
}
},// end success
error: function(jqXHR, textStatus, errorThrown)
{
console.log(errorThrown);
}
});//end ajax.
}); //end document ready
When I check the console, I see that the request failed with the following error message:
Uncaught SyntaxError: Unexpected token :
It's basically dying when it sees the first ":" after the mongo doc id "5717a3873747106e60a087ec"
What I've tried:
I've validated the return data in jsonlint and it looks like the data is valid json.
I'm not sure what else to check. I'm playing around with jsonp vs json for the data type of my ajax call. But other than that, I'm out of ideas.
Any suggestions?
EDIT 1
I should also say that I found this post: jQuery.ajax() call is returning JSON.parse unexpected character error
but i don't know how to edit the json headers. I'm currently also playing around with the code to see how to do this
EDIT 2:
When I change jsonp to json, I get another error message :
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
The variable "request" contains something like this:
http://10.1.1.1/mytestapp/index.php/export/get_widgets/available
I thought the problem was that the above mentioned URL is being compared with the "http://localhost/mytestapp" URL that i used to launch the application. So tried to launch my web app like this instead:
http://10.1.1.1/mytestapp/index.php/
but it still fails with the same error.
dataType:'json',notdataType:'jsonp',somecallback("json data here"). if you're getting back just the json data itself, with no function call wrapper, then it's not jsonp.statuskey in the returned json, but that will be the next error after you fix the data type./mytestapp/....