Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法使用cordova发送ajax请求_Javascript_Jquery_Ajax_Cordova - Fatal编程技术网

Javascript 无法使用cordova发送ajax请求

Javascript 无法使用cordova发送ajax请求,javascript,jquery,ajax,cordova,Javascript,Jquery,Ajax,Cordova,我启动了一个新项目,并为请求添加了代码。我的js文件如下所示: (function () { "use strict"; document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false ); function onDeviceReady() { $.ajax({ type: "GET", url: "http://api.vk.com/method/databas

我启动了一个新项目,并为请求添加了代码。我的js文件如下所示:

(function () {
"use strict";

document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

function onDeviceReady() {

    $.ajax({
        type: "GET",
        url: "http://api.vk.com/method/database.getCountries?v=5.5&need_all=0",
        crossDomain: true,
        success: function (response) {
            alert('Works!');
        },
        error: function (data) {
            console.log(data);
            alert('Not working!');
        }
    });

    // Handle the Cordova pause and resume events
    document.addEventListener( 'pause', onPause.bind( this ), false );
    document.addEventListener( 'resume', onResume.bind( this ), false );

    // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
    var parentElement = document.getElementById('deviceready');
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');
    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');
};

function onPause() {
    // TODO: This application has been suspended. Save application state here.
};

function onResume() {
    // TODO: This application has been reactivated. Restore application state here.
};
})();
但它根本不起作用。它总是返回“不工作”。我尝试了其他发送请求的方法,但总是出现错误。
求求你,救命

默认情况下,Cordova 5.0阻止外部http请求

尝试添加到您的项目中

并且重建你的应用程序仍然存在问题。执行以下操作之一:

1) 在响应服务中设置以下herder:

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
2) 将此行添加到配置文件-:

<access origin="*" />
4) 您可以使用$.getJSON来生成请求,而不是ajax


希望它能帮助您。

谢谢您的回答,但它仍然不起作用。我执行了您编写的这些步骤,没有任何更改。您能复制ajax调用时得到的错误吗?ajax正在返回状态为0的对象,但这并不重要。我解决了这个问题。我决定使用插件“cordova HTTP”,这对我很有帮助。我不知道它是如何工作的,但它工作)无论如何,谢谢你的帮助
$.support.cors=true;