【问题标题】:phonegap http error 404phonegap http错误404
【发布时间】:2014-04-08 10:15:55
【问题描述】:

我正在尝试使用 phonegap 应用程序中的 ajax 调用连接到服务器以获取数据。 但是对于我尝试过 google.com、10.0.2.2(android 上的本地主机)的每个请求,我都会收到状态 404。

这是我创建应用程序所遵循的步骤

我使用 phonegap 命令行创建了模板应用程序 phonegap create my-app

使用phonegap run android 运行应用程序(这会在模拟器上显示“设备准备就绪”屏幕)

添加了以下代码,用于对 index.js 进行 Ajax 调用 - receivedEvent 函数

var xmlhttp =  new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
    console.log('rdy state - '+ xmlhttp.readyState);
    console.log('status - '+ xmlhttp.status);
    console.log('text status - '+ xmlhttp.statusText);
    console.log('Response Text - '+ xmlhttp.responseText);
}
xmlhttp.open("GET","http://10.0.2.2/", true);
xmlhttp.send();

我可以从模拟器浏览器访问http://10.0.2.2

这是我正在使用的环境

Phonegap 版本 - 3.4.0-0.19.8 Android 版本 - 4.4.2

-- 维沙尔

【问题讨论】:

    标签: android ajax cordova


    【解决方案1】:

    您是否在 config.xml 中放置了访问令牌?试着放

    <access origin="*"/>
    

    到你的config.xml

    * 提供对所有域的访问权限。 查找http://docs.phonegap.com/en/3.4.0/guide_appdev_whitelist_index.md.html#Whitelist%20Guide 了解更多信息。

    【讨论】:

    • 我在使用 Cordova 时遇到了这个问题,但我有通配符。有什么想法吗?
    【解决方案2】:

    尽管你有:

    <pre> <access origin="*"/> </pre> in config.xml 
    

    您需要将白名单添加到您的 phonegap(cordova) 项目中:

    phonegap plugin add cordova-plugin-whitelist 
    

    在此之后,您的应用将不再提供 404。

    【讨论】:

      猜你喜欢
      • 2012-08-30
      • 2011-12-09
      • 2023-03-19
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多