我有一个 javascript 页面,应该将用户输入的用户名发送到服务器上的 php 脚本。javascript 页面来自http://192.168.1.4/login.html,它尝试访问http://192.168.1.4/GetInfo.php的 php 脚本。我认为我无法从 javascript 页面访问 php 脚本中的用户名,因为 firefox 中的相同来源策略但是,我不确定如何确认这种怀疑,所以如果我错了,请原谅我。我才刚刚开始学习 javscript 和 php。我想知道当时是否有不同的方式来传递这些信息。代码如下。谢谢!
的JavaScript:
<html>
<head>
<title>Login Page for SplitAuth</title>
</head>
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script language="Javascript">
function getUsername()
{
var username = window.prompt('Please Type Your Username');
var temp = document.getElementById('temp');
temp.innerHTML = username;
jQuery.ajax(
{
type: "POST",
url:"GetInfo.php",
data: username,
success: function(msg)
{alert("data Saved: "+msg);}
});//ends the jQuery send
}//ends the GetUsername function
</script>
<body onLoad=getUsername()>
<div id="temp">This will show text</div>
<body>
</html>
php脚本:
<?
$inFile="MyID.config.php";
$handle=fopen($inFile, 'r') or die ("No credentials could be gotten because the file MyID.config.php would not open.");
echo $_POST['msg'];
fclose($fh);
?>