gpt4 book ai didi

php - Post 请求中未定义索引

转载 作者:行者123 更新时间:2023-12-01 04:48:53 24 4
gpt4 key购买 nike

我不知道为什么会收到这个奇怪的错误!

PHP Notice: Undefined index: refId in /var/www/echo.php on line 5

我正在获取控制台输出,但无法回显 refId。我在这里做错了什么吗?

<?php
$rollUrl = 34;
$refId = $_POST['refId'];
echo $refId;
?>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$.ajax({
url:'echo.php',
type: 'POST',
data: { 'refId': "<?php echo $rollUrl ?>" },
success: function(response){
console.log('Getting response');
}
});
</script>

最佳答案

请参阅下面代码中的注释:

<?php
$rollUrl = 34;

//Only try to process POST if there is something posted *and* refId exists
if (count($_POST) > 0 && isset($_POST['refId'])) {
$refId = $_POST['refId'];
echo $refId;
//Exit after echoing out the refId so that the HTML below does not also get returned.
exit();
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$.ajax({
url:'echo.php',
type: 'POST',
data: { 'refId': "<?php echo $rollUrl ?>" },
success: function(response) {
//Updated log to show the actual response received.
console.log('Getting response of "' + response + '"');
}
});
</script>

当我测试时没有抛出任何错误并且 Ajax 正在执行时,这对我有用。

关于php - Post 请求中未定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24360010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com