OK, I have this piece of code,
var record = new Object(); //var aData = {};でもOK
record["deviceID"] = "123456"; //aData["name"] = "hoge"でもoK
$.getJSON("http://192.168.2.10:8080/commapi/comaction/init", {record: JSON.stringify(record)},
function(objRes){
alert(objRes["response"]); //objRes["country"]でもok
});
I just made a simple servlet that simply replies back the deviceID I put. But my problem is there is an error like
XMLHttpRequest cannot load http://192.168.2.10:8080/commapi/comaction/init?record=%7B%22deviceID%22%3A%22123456%22%7D. Origin file:// is not allowed by Access-Control-Allow-Origin.
how can I fix this? Thank you!
UPDATED SCRIPT
I tried using the JSONP, and my resulting script looks like this
$(document).ready(function() {
$("#ui-2").click(function(){
getJSON("http://192.168.2.143:8080/commapi/comaction/init?record=%7B% 22deviceID%22%3A%22123456%22%7D&callback=loaded");
});
};
function getJSON(url){
var s = document.createElement('script');
s.setAttribute('src',url);
document.getElementsByTagName('head')[0].appendChild(s);
// Loading ..
$("results").innerHTML = '<p>Loading …</p>';
};
function loaded(data) {
var res = data.query.results.item;
var html = "";
var i = 0;
var y = res.length;
for(i; i<y; i++) {
alert(data.deviceID);
}
};
but this is the new error
Resource interpreted as Script but transferred with MIME type application/json.
We are not allowed to do
Cross-site HTTP requestsfrom JavaScript. That is, you are only allowed to make AJAX requests to your own requestee domain only.You can only be successful with
http://192.168.2.10:8080/commapi/comaction/initAJAX call if the AJAX call is made from the same domain ashttp://192.168.2.10:8080.As a workaround, this is often accomplished by
JSONPwhich passes aJavaScript callback functionwhich is a JavaScript function located on your page and requests to the server where the server wraps theJSONdata with callback function. This is executed by creating a<script></script>tag withsrcattribute to be theAJAXurl and appended to the HTMLbody.PS: You server must have capability to wrap your output data which will require to change on your
http://192.168.2.10:8080/commapi/comaction/initcodes.