I have looked up on all possible options and solutions in the last 24 hours and I'm still stuck with the error. Here is the thing, the file is created dynamically (in JAVA) and it is an external URL. The file type is application/json
and it looks like this:
{
"main": [{
"title": "90 days",
"startDate": "2016-06-16",
"endDate": "2016-09-14",
"productName": "Main",
"product": "main",
"price": 398,
"phone": null,
"type": "Main",
"campaign": null
}, {
"title": "90 days",
"startDate": "2016-06-16",
"endDate": "2016-09-14",
"productName": "Main",
"product": "main",
"price": 398,
"phone": null,
"type": "Main",
"campaign": null
}],
"extra": [{
"title": "extra",
"startDate": "2016-08-14,22:00:00",
"endDate": "2016-09-12,22:00:00",
"productName": "extra",
"product": "extra",
"price": 29,
"phone": null,
"type": "Extra",
"campaign": ""
}, {
"title": "extra",
"startDate": "2016-07-15,22:00:00",
"endDate": "2016-08-13,22:00:00",
"productName": "extra",
"product": "extra",
"price": 29,
"phone": null,
"type": "Extra",
"campaign": ""
}]
}
Now I tried several combinations, none of them worked and I'm stuck with the following.
var myData= {};
$.ajax({
url: 'http://__external-URL__/product', /* no extension */
dataType: 'jsonp',
jsonpCallBack: 'callback', /* tried function() { alert('foo'); } */
type: 'GET',
contentType: "application/json",
async: false, /* tried true as well as totally removing it */
success: function (data) {
myData = $.parseJSON(data);
console.log('success ' + myData);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('jqXHR: '+ jqXHR);
console.log('textStatus: '+ textStatus);
console.log('errorThrown: '+ errorThrown);
}
});
The response is:
Uncaught SyntaxError: Unexpected token :
jqXHR: [object Object]
textStatus: parsererror
errorThrown: Error: jQuery19005705788836383217_1467193897750 was not called
As per the browser > inspect > Network:
General:
Request Method:GET
Status Code:200 OK
and
Response Header:
Content-Type:application/json
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
What is wrong with my code? Thank you in advance for the solution.
If I change the datatype to JSON
and remove async: false
, i get:
XMLHttpRequest cannot load external-URL/product. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'external-URL' is therefore not allowed access. The response had HTTP status code 403.