【发布时间】:2012-05-02 12:00:48
【问题描述】:
我有一个 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);
?>
【问题讨论】:
-
在浏览器中访问192.168.1.4/GetInfo.php会得到什么?
-
php 脚本可以直接运行吗?
-
由于 javascript 运行客户端,
192...将无法工作,除非您运行 ajax 请求的机器是服务器本身。data: username也是错误的——我认为应该是data: "username=" + username——但是你也没有在 PHP 中引用它。你的 PHP 代码完全没有意义。很难说你在做什么。 -
让人想知道..那篇文章的年龄,您从该文章中复制粘贴了脚本
-
@tereško 实际上是我自己写的,因为我说我正在学习,所以下次或者发表这样愚蠢的评论,说些建设性的东西,或者什么都不说
标签: php javascript jquery same-origin-policy