【发布时间】:2017-11-19 06:37:59
【问题描述】:
我正在 phonegap 上启动一个网络应用程序,并且我正在测试远程服务器连接。你猜怎么着,它不起作用!我不知道为什么。 它适用于浏览器,但不适用于我从脚本制作的 phonegap 应用程序。我查看了互联网、论坛等,但没有找到任何解释。也许是因为我使用的是互惠服务器(OVH.FR),但我不确定......
帮助!谢谢!
这是我的文件:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Test software</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script src="./req/jquery/jquery/jquery-2.1.0.min.js"></script>
<script src="./req/jquery/mobile/jquery.mobile-1.4.5.min.js"></script>
<link rel="stylesheet" type="text/css"
href="./req/jquery/mobile/jquery.mobile-1.4.5.min.css" />
<script>
/* ------------------------------------------------ */
$(document).ready(function(){
$.ajax({
url : 'http://test.dexterin.com/intro/introConnexion.php',
cache: false,
type : 'POST',
dataType : 'xml',
success : function(xml) {
$("#mainPageContent").append("<p>Connected !</p>");
},
error : function(xml) {
$("#mainPageContent").append("<p>No response from ajax</p>");
}
}); // Fin ajax
});
/* ------------------------------------------------ */
</script>
</head>
<body>
<div data-role="page" data-theme="a" id="mainPage">
<div role="content" class="ui-content" id="mainPageContent">
<p>Initialisation du logiciel ...</p>
</div>
</div>
</body>
</html>
config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "com.phonegap.example"
versionCode = "10"
version = "0.0.1" >
<!-- versionCode is optional and Android only -->
<name>TEST App</name>
<description>
A test for phonegap app
</description>
<author href="https://build.phonegap.com" email="support@phonegap.com">
Hardeep Shoker
</author>
<access origin="http://test.dexterin.com/" subdomains="true" />
</widget>
introConnexion.php
<?
session_start();
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST,GET"); // ,OPTION
header("Access-Control-Allow-Headers: content-type");
header("Access-Control-Allow-Headers: NCZ");
header("Content-type: text/xml;charset=utf-8");
date_default_timezone_set('France/Paris');
echo "<?xml version = \"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n";
?>
<root>
<element>OK</element>
</root>
【问题讨论】:
-
这不是我对标头所做的('Access-Control-Allow-Origin: *');在 introConnexion.php 文件中? (远程服务器上的文件)
-
我使用了一个函数 getXDomainRequest() : xdr = new XDomainRequest();然后: xdr.onload = function() {alert("Response : " + xdr.responseText);} xdr.open("GET", "test.dexterin.com/intro/introConnexion.php"); xdr.send(); 它返回一个 xdr.responseText 但是空...
-
你可能是对的!我在 config.xml 中添加了白名单插件(见下文),并添加了命令 $.support.cors = true;在 jquery 函数中。感谢您的帮助!!
标签: php ajax cordova server phonegap