我尝试使用CORS
使用this有趣的问题,但似乎无法正常工作我在同一网络中有两个服务器我使用https
协议并使用PHP
使用Jquery
Ajax,请点击我的简单页面:
提出请求的PHP页面:
<?php
include("menu.php");
echo "<script type='text/javascript' src='js/test_remoto.js'></script>";
echo "<p>questo e ció che leggo da remoto</p>";
echo "<button id='testa'>TESTA</button>";
echo "<div id='result'></div>";
?>
Jquery代码:
$(function(){
$("#testa").click(function(){
//alert("qua arrivo!");
$.post('https://my_remote_server/test_remoto.php',
{firstname:'Jeff'},
function(xml){
if ($("status", xml).text()==1)
{
alert("OK");
} else
alert("NO");
},"xml");
});
});
远程服务器上的PHP页面:
<?php
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
header("Content-type: text/xml");
header("Cache-Control: no-cache");
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
echo "<risultato>\n";
if ($_POST["firstname"]=="Jeff")
echo "<status>1</status>\n";
echo "</risultato>\n";
?>
Firebug NET回复:
Accept application/xml, text/xml, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Cache-Control no-cache
Connection keep-alive
Content-Length 14
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Host my_remote_server
Origin https://my_local server
Pragma no-cache
Referer https://my_local_server/test_remoto.php
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0 FirePHP/0.7.4
x-insight activate
稍微更新我从浏览器网络配置中删除了代理,现在从firebug net选项卡中我在远程文件状态字段中中止。
先谢谢你的帮助,ciao h。