Javascript PhoneGap中的$.getJSON不工作
运行上面的脚本时,我看到以下警报:Javascript PhoneGap中的$.getJSON不工作,javascript,jquery,linux,ubuntu,cordova,Javascript,Jquery,Linux,Ubuntu,Cordova,运行上面的脚本时,我看到以下警报:失败!错误:。error参数没有值,因此我很难找到问题的实际位置。大多数其他具有此标题的问题的最佳答案是不添加端点的域,我可以排除: 要查看我的config.xml文件,请单击 您将看到我设置了origin=“*”,它应该允许与所有端点进行通信,而不管是哪个域。你知道为什么会这样吗 更新 这与我的服务器有关,因为其他服务器显示“成功”警报消息。不过,仍然没有找到具体的问题。出于某种原因,我的服务器似乎不喜欢跨源请求。。。我已禁用防火墙,但没有成功。我只想确认请求
失败!错误:
。error
参数没有值,因此我很难找到问题的实际位置。大多数其他具有此标题的问题的最佳答案是不添加端点的域,我可以排除:
要查看我的config.xml文件,请单击
您将看到我设置了origin=“*”
,它应该允许与所有端点进行通信,而不管是哪个域。你知道为什么会这样吗
更新
这与我的服务器有关,因为其他服务器显示“成功”警报消息。不过,仍然没有找到具体的问题。出于某种原因,我的服务器似乎不喜欢跨源请求。。。我已禁用防火墙,但没有成功。我只想确认请求在同一个URL上工作正常
已启用服务器站点/000-default.conf:
var app = {
// Application Constructor
initialize: function() {
$.getJSON( "http://domain.com/api/data")
.done(function( json ) {
alert("SUCCESS!");
})
.fail(function( jqxhr, textStatus, error ) {
alert("Failure! Error: " + error);
});
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
标题添加访问控制允许源“*”
标题添加访问控制允许方法“获取,选项”
服务器管理员webmaster@localhost
DocumentRoot/var/www/html
ErrorLog${APACHE_LOG_DIR}/error.LOG
CustomLog${APACHE\u LOG\u DIR}/access.LOG组合
此问题的解决方案:
在服务器上的.htaccess中,添加此行
<VirtualHost *:80>
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods "GET, OPTIONS"
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
服务运行在哪个web服务器上?尝试将允许标头设置为的选项service@MortenOC运行Ubuntu12.04,Apache2.4.7,将立即尝试,并在几秒钟内通知您,thanks@MortenOC使用我的VirtualHost配置编辑的问题。。。启用标题等,没有运气,仍然失败:-(嗯,奇怪。你看到这个了吗?:@MortenOC刚看了一眼,但不确定我是否理解其中的关联性,开始失去生存的意愿,在过去的4个小时里一直在兜圈子(
Header set Access-Control-Allow-Origin "*"