gpt4 book ai didi

javascript - Ajax调用成功但$_POST ['value']为空

转载 作者:行者123 更新时间:2023-11-28 10:38:55 26 4
gpt4 key购买 nike

问题已解决

The issues seemed to be browser related. Tried with a different system and it works fine.

我正在编写一个实时搜索,以使用 ajax 和 php 显示数据库中用户的详细信息。表单字段的输入已正确传递到 ajax 文件。我检查了 console.log() 并且它打印正确。之后,Ajax 调用不会出现任何错误。但是当尝试回显 $_POST['search'] 时,它会打印空白区域。

searchform.php

<input type="text" placeholder="Search" class="search"/>
<div class="search-result"></div>

search.js

    $(function(){
$('.search').keyup(function(){
var search = $(this).val();
//console.log(search);
$.ajax({
url:'search.php',
data:{search:search,},
type:'POST',
success:function(data){
console.log(data);
$('.search-result').html(data);
},
error:function(){
console.log("error");
}
});
});
});

search.php

 <?php

include('core/init.php');
echo $_POST['search'];

?>

已编辑

我解决了这个问题,但不知道原因,也没有找到解决方案。基本上, include('core/init.php'); 没有被包含在内。我尝试了 var_dump(include('core/init.php')); 并且它返回 bool(false)。路径是正确的。使用ajax调用包含includes的文件时是否需要做一些事情?

最佳答案

First, you change this data:{search:search,} to this data:{search:search}

Second, try to change your echo in your search.php become this echo json_encode($_POST['search']);

For more complex checking, you can inspect element (press F12 from your browser), go to network tab and open in new tab your search.php file. It is show your expected value?

If search.php shown the expected value, try to hard refresh your browser in windows (ctrl + F5) to try updated your js

关于javascript - Ajax调用成功但$_POST ['value']为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55116891/

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