我正在尝试将嵌入在html中的Flash文件部署到Google应用引擎中。 Flash(动作脚本2.0)使用“post”方法发送主机名并通过php函数gethostbyname()获取其ip地址。 事实上,我知道谷歌应用程序引擎不支持PHP。 所以我尝试用另一种方式在其他免费网络服务器中部署ipPHP.php,在谷歌应用引擎中只使用flash文件。 但它不起作用,我不知道为什么。 你能给我一个这个问题的小费吗?
-------------- domaintoip.fla ---------------------
result_lv = new LoadVars();
result_lv.byname = _root.domainnm;
trace("Sending... " + result_lv.byname);
result_lv.onLoad = function (success)
{
if (success)
{
_root.ip = unescape(this.result);
trace("Return value from the PHP : " + unescape(this));
if(_root.ip.length==5){
_root.flag=1;
}
else{
var mystring=_root.ip;
arr=mystring.split(".");
_root.ipby1=arr[0];
_root.ipby2=arr[1];
_root.ipby3=arr[2];
if(arr[3].length==15)
{
_root.ipby4=arr[3].substr(0,3);
}
if(arr[3].length==14)
{
_root.ipby4=arr[3].substr(0,2);
}
if(arr[3].length==13)
{
_root.ipby4=arr[3].substr(0,1);
}
_root.flag=0;
}
}
else
{
trace("Cannot call the PHP file...");
_root.flag=1;
}
}
result_lv.sendAndLoad("http://anotherserver../ipPHP.php", result_lv, "POST");
-------------- ipPHP.php ---------------------
<?php
$Var1 = $_POST['byname'];
$rtnValue = gethostbyname(trim($Var1));
if(ip2long($rtnValue) == -1 || $rtnValue == $Var1 ) {
$rtnValue =0;
echo (result=$rtnValue");
}
else {
echo("result=$rtnValue");
}
&GT;
答案 0 :(得分:1)
如果您的网站托管在应用引擎上,由于 Same Origin Policy ,您无法向应用引擎以外的主机发出AJAX呼叫。这种限制通常是正确的,并不是特定于app引擎的。为了概括,对于在域X
托管的任何网页,该网页无法向域Y
发出AJAX请求。
你实际上遇到了一个更基本的问题:当你拥有的唯一工具是锤子时,每个问题看起来都像钉子。事实上,您可以使用doPost
方法轻松地使用应用引擎处理POST请求,并且您可以非常类似于PHP脚本获取客户端的IP地址。这里绝对没有理由使用PHP;你已经设置了一个全新的服务器来调用一个内置的PHP函数?那太疯狂了;你可以用app引擎servlet做同样的事情。
请考虑以下代码:
public void doPost(HttpServletRequest request,HttpServletResponse response) {
/* get "byname" param, equivalent to $POST['byname'] */
String rtnValue = request.getParameter("byname");
/* TODO: your if statements and other logic */
/* print response to client, equivalent to your echo statement */
response.getWriter().print("result=" + rtnValue);
}