2016-12-30 82 views
0

我正在为Cordova/Phonegap构建iOS应用程序。科尔多瓦/ Phonegap:AJAX调用不适用于iOS设备

我有一个登录表单,它通过将用户名+密码传递给我的服务器上的PHP脚本通过AJAX调用工作。

当我通过我的PC上的本地主机(Chrome浏览器,具体)运行它时,它工作得很好。 AJAX调用被传递到PHP,PHP运行,并返回响应。

当我通过Phonegap测试应用程序在iOS设备上运行同一实例时,没有任何URL /表单提交。没有任何基于网络的作品。我在index.html页面上有一个来自外部来源的图像,但这并没有在iOS上加载。

我想这将是一个权限问题,所以我改变了标题为以下:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-eval' data: blob: filesystem: ws: gap: file: cdvfile: https://ssl.gstatic.com *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: 'unsafe-inline'; connect-src * 'unsafe-inline'; child-src *; "> 

我还添加了我的config.xml中的以下内容:

<allow-navigation href="*" /> 
<allow-intent href="*" /> 
<access origin="*"/> 

的想法是这会让所有流量都通过(仅用于测试目的 - 我知道从安全角度来看这不是一个好主意)。

index.html中的图片,图片是从一个HTTP源,它没有被更早呈现拉,我的iOS设备,这是伟大的现在的工作,因为这意味着权限的变化有所帮助,但不幸的是, AJAX仍然无法正常工作。

下面是我在我的index.html:

<html> 
<head> 
    <script type="text/javascript" src="cordova.js"></script> 
    <script type="text/javascript" src="scripts/scripts.js"></script> 
    <meta charset="utf-8" /> 
    <meta name="format-detection" content="telephone=no" /> 
    <meta name="msapplication-tap-highlight" content="no" /> 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" /> 
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-eval' data: blob: filesystem: ws: gap: file: cdvfile: https://ssl.gstatic.com *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: 'unsafe-inline'; connect-src * 'unsafe-inline'; child-src *; "> 
    <link rel="stylesheet" type="text/css" href="style/bootstrap.min.css"> 
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> 
    <link rel="stylesheet" type="text/css" href="style/leaf.css"> 
    <title>App Name</title> 
</head> 
<body onload="onLoad()"> 


    <img style="width:25%; padding-top:2em;" src="img/logo.png" /> <!-- Local image, which works fine on all devices --> 
    <img style="width:25%; padding-top:2em;" src="http://www.userlogos.org/files/logos/Karmody/alaTest_Iphone01a.png" /> <!-- external image which didn't work earlier but works now since I changed the header permissions --> 
    <form id="loginForm" onsubmit='return false;' action="http://LINK TO SERVER/login.php" role="form" method="post" > 

     <input type="text" name="username" id="username"> 


     <input type="password" name="password" id="password"> 


     <input type="submit" name="submit" value="Login"> 

    </form> 

    <h3><a href='http://google.com'>Link to page (Works in browser but not in mobile)</a></h3> 


</body> 
</html> 

这是我的脚本文件:

function onLoad() { 
    document.addEventListener("deviceready", onDeviceReady, false); 
} 
function onDeviceReady() { 
    console.log(device.uuid); 
} 

function onDeviceReady() { 
    $('#loginForm').submit(function(){ 
     var username = $("[name='username']").val(); 
     var password = $("[name='password']").val(); 
     var dataString = {username: username,password: password}; 
     $.ajax({ 
      type: "POST", 
      url: "http://LINK TO SERVER/login.php", 
      data: dataString, 
      dataType: 'json', 
      crossDomain: true, 
      cache: false, 
      success: function(responseBack){ 
       if(response == "success"){alert("Successfully authenticated.");} 
       else{alert("Username/password incorrect.");} 
      } 
     }); 


    }); 

} 

我什么,我应该为了获得网络解决任何提示来电工作?

谢谢!

回答

0

通过启用jQuery Cors修复了问题。

$.support.cors = true; 
相关问题