I was using jQuery's getJSON method and a method in Extjs to get some json data from server.
The Javascript code is put on apache2 on server A and the web service is put on tomcat7 on server A. But when I use host B to visit the javascript, and the a method in javascript code calls the web service. the request is made from host B to server A, right?
But I get an error:
XMLHttpRequest cannot load http://192.168.5.107:8080/restful/product/all/?callback=?&_dc=1384439969320&page=1&start=0&limit=25. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.5.107' is therefore not allowed access.
And I have to add the
?callback=?
to my url.
But I get another error called "Uncaught SyntaxError: Unexpected token : " with my jQuery code. I googled this and most of the persons' suggestion is to remove the callback. But I have to use it, or I cannot get the JSON data.
Here's my code using jQuery:
$.getJSON('http://192.168.5.115:8080/restful/standard/id/' + id, function(data) {
console.log('old data:\n');
console.log(data); // use data as a generic object
oldObj = data;
var newObj = Object.keys(oldObj).map(function(k) {
return { key: k, value: oldObj[k] };
});
console.log('new data:\n');
console.log(newObj); // use data as a generic object
});
And even I add the callback parameter. The code in Extjs still get the first error.
Here's my Extjs code:
// create the data store
var store = Ext.create('Ext.data.Store', {
model: 'Product',
proxy: {
type: 'ajax',
url : 'http://192.168.5.115:8080/restful/product/all/?callback',
reader: {
type: 'json'
}
}
});
I have tried to fix his for three days and got nothing. I really wish someone can help me solve it.