我想从服务器获取远程数据,但这会导致连接未执行的错误。
在Chrome浏览器的控制台中显示。
Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access.
XMLHttpRequest cannot load http://domeain.com/json.txt. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access.
我的代码是
function getTodoList (){
var sendit = Ti.Network.createHTTPClient();
sendit.open('GET', 'http://domain.com/json.txt');
sendit.onload = function(){
var json = JSON.parse(this.responseText);
var json = json.todo;
//if the database is empty show an alert
if(json.length == 0){
$.tableView.headerTitle = "The database row is empty";
}
dataArray = [];
for( var i=0; i<json.length; i++)
{
var row = Ti.UI.createTableViewRow({
title: json[i].todo,
hasChild : true,
});
dataArray.push(row);
}
$.tableView.setData(dataArray);
};
sendit.onerror = function(){
Ti.API.debug(e.error);
alert('There was an error during the conexion');
};
sendit.setTimeout(10000); // 10 sec for timeout
sendit.setRequestHeader("Content-Type", "application/json; charset=utf-8");
sendit.setRequestHeader("Access-Control-Allow-Origin", "*");
sendit.send();
}
当我提供像sendit.open这样的本地文件时('GET','json.txt'); json.txt在我的资产文件夹中。 然后它的工作正常。 但我想从远程服务器获取数据。 如何解决这个问题请回复谢谢