[英]javascript post to .php file one server does not work - manual entry of URL in browser to same .php works
i had some issues with understanding how to get javascript (client) variables transferred so they were acessible from php (serverside) as session : get an iframe's "src" value in PHP? 我有一些问题,了解如何转移javascript(客户端)变量,所以他们可以从PHP(服务器端)访问作为会话: 在PHP中获取iframe的“src”值?
Now im in a situation where i use firebug to try to debug whats going on, but it just doesnt make sense : 现在我处于一种情况,我使用firebug尝试调试正在进行的,但它只是没有意义:
i have this function to update an iframe and i want to pass on the page that that iframe is displaying : 我有这个功能来更新iframe,我想传递iframe正在显示的页面:
function frameclick(pageurl)
{
$.post("session_write.php?",
{
frameurl : pageurl
}
$("#iFrame1").attr('src', pageurl);
console.log ('<?php echo "logout:".$langpath.$_SESSION['frameurl'];?>');
}
pageurl is ex. pageurl是ex。 "/lang/en/new.htm" - and if i inspect it with firebug i also can see it says that it passes it correctly ( also with conversion of /).
“/lang/en/new.htm” - 如果我用firebug检查它,我也可以看到它说它正确传递它(也转换为/)。
my script serverside that its posted to is like this : 我发布的脚本服务器端是这样的:
#session_write.php
<?php
session_start();
print_r($_GET['frameurl']);
if (isset($_GET['frameurl']))
{
$_SESSION['frameurl'] = $_GET['frameurl'];
print_r($_SESSION);
}
?>
Posting to that php script on the server will fail via the javascropt - $_SESSION['frameurl'] will be '', but if i ex. 发布到服务器上的PHP脚本将通过javascropt失败 - $ _SESSION ['frameurl']将是'',但如果我是ex。 do it manually like this : (http):
像这样手动执行:(http):
//localhost/phpmenu/session_write.php?frameurl=lang%2Fen%2Fnew.htm
then it will be correctly set in the $_SESSION["frameurl"] variable. 然后它将在$ _SESSION [“frameurl”]变量中正确设置。
I simply cannot understand whats different between doing the javascript post and doing it manually in the browser and why its causing me this problem ? 我简直无法理解在javascript帖子和在浏览器中手动执行它之间有什么不同以及为什么它会导致我这个问题?
anyone with an idea ? 有想法的人吗? thanks
谢谢
You are using .post
, which executes a POST request, but when you type in the URL in the address bar, that is a GET request. 您正在使用
.post
执行POST请求,但是当您在地址栏中键入URL时,这是一个GET请求。
$_GET
retrieves any params passed through GET, while $_POST
retrieves any params passed through POST. $_GET
检索通过GET传递的任何参数,而$_POST
检索通过POST传递的任何参数。 So if you use .post
with Javascript but try to retrieve with $_GET
in PHP, it wouldn't work. 因此,如果您使用
.post
与Javascript但尝试使用PHP中的$_GET
进行检索,则无效。
When you POST variables to a PHP file, $_GET
is not set. 将变量POST到PHP文件时,未设置
$_GET
。 Use $_POST['frameurl']
instead. 请改用
$_POST['frameurl']
。 Also, it looks like you're missing a close paren in frameclick
to end the post
call. 此外,看起来你在frameclick中错过了一个关闭
frameclick
来结束post
调用。
You are passing data via a POST
request and retrieving for all the GET requests. 您正在通过
POST
请求传递数据并检索所有GET请求。 Use $_POST
instead. 请改用
$_POST
。 You may also be interested in $_REQUEST
您可能也对
$_REQUEST
感兴趣
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.