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();

运行上面的脚本时,我看到以下警报:"Failure! Error: "error参数没有值,因此我无法找到问题的实际位置。这个标题的大多数其他问题的最佳答案是不添加端点的域,我可以排除:
要查看my config.xml文件,请单击here
您将看到我设置了origin="*",它应该允许与所有端点进行通信,不管是哪个域。你知道为什么会这样吗?
更新
这与我的服务器有关,因为其他服务器显示“成功”警报消息。不过,仍然没有找到具体的问题。出于某种原因,我的服务器似乎不喜欢跨源请求。。。我已经禁用了防火墙,但没有成功。只想确认请求在同一个URL上运行良好。
服务器站点已启用/000-default.conf:
<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>

最佳答案

解决这个问题的办法是:
在服务器上的.htaccess中,添加以下行

Header set Access-Control-Allow-Origin "*"

09-30 13:56
查看更多