gpt4 book ai didi

php - 尽管我可以在 firebug post、html 和响应选项卡中看到 $_POST 数据,但 $_POST 为空

转载 作者:行者123 更新时间:2023-12-01 05:34:41 26 4
gpt4 key购买 nike

因此,我获取 jquery 日期选择器和下拉选择菜单的状态,并尝试使用 AJAX 将这两个变量发送到另一个 php 文件。

var_dump($_POST);

网页上的结果:

    array(0) {
}

但是,当我查看 Firebug 中的 Net 面板时,我可以看到 POST 和 GET url,它显示了 Post、Response 和 HTML,所有这些都显示了我发送到 PHP 文件的变量,但是在转储时,它页面上不显示任何内容。

我一直在研究 SO 上的其他类似问题,这导致我更改 php.ini 文件以增加帖子大小并更新我的 ajax 调用以使用 json 对象,然后在 php 端解析它。

目前我只是想传递一个字符串来工作,我的代码如下所示:

AJAX:

  $("#submit_button").click(function() {
// get date if selected
var selected_date = $("#datepicker").datepicker("getDate");
// get show id if selected
var selected_dj = $("#show-list").val();
// put the variables into a json object
var json = {demo : 'this is just a simple json object'};
// convert to json
var post_data = JSON.stringify(json);
// now put in variable for posting
var post_array = {json : post_data};

$.ajax({
type: "POST",
url: template_dir + "/get-show-logs.php",
data: post_array,
success: function(){
alert("Query Submitted");
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});

// clear div to make room for new query
$("#archived-posts-container").empty();
// now load with data
$("#archived-posts-container").load(template_dir + "/get-show-logs.php #get_logs");

});

现在这是从 .load() 调用运行的 php,我试图在其中访问 $_POST 变量:

获取-show-logs.PHP:

<div id="get_logs">

<?php

if(isset($_POST["json"])){
$json = stripslashes($_POST["json"]);
$output = json_decode($json);

echo "im here";
var_dump($output);

// Now you can access your php object like so
// $output[0]->variable-name
}

var_dump(getRealPOST());


function getRealPOST() {
$pairs = explode("&", file_get_contents("php://input"));
$vars = array();
foreach ($pairs as $pair) {
$nv = explode("=", $pair);
$name = urldecode($nv[0]);
$value = urldecode($nv[1]);
$vars[$name] = $value;
}
return $vars;
}

?>

</div>

您可以看到我正在尝试访问 $_POST 变量,并且 isset 检查没有通过(页面没有回显“im here”),然后我还尝试解析输入我自己,那也是空的。

页面上的输出如下所示:

数组(1){[""]=>字符串(0)""}

但是,Firebug Net 面板在“响应”选项卡下再次显示以下内容:

    <div id="get_logs">

im hereobject(stdClass)#1 (1) {
["demo"]=>
string(33) "this is just a simple json object"
}
array(1) {
["json"]=>
string(44) "{"demo":"this is just a simple json object"}"
}

</div>

我不确定是什么原因导致了这个问题,Firebug 可以看到它,但 php 文件看到一个空数组。

现在我对使用 ajax 和 $_POST 等还很陌生,所以如果你读到任何你不是 100% 确定的内容,不要以为我对此一无所知!说出来!哈哈。另外,我在本地主机上使用 MAMP 执行此操作,因此我不确定这是否会导致任何问题。

感谢您提前提供的帮助!

最佳答案

您当前没有在 AJAX 调用中使用响应。请参阅此示例,它将把返回的响应输出到控制台。

$.ajax({
type: "POST",
url: template_dir + "/get-show-logs.php",
data: post_array,
success: function(response){
console.log(response);
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});

Success
Type: Function( Anything data, String textStatus, jqXHR jqXHR ) A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object.

-http://api.jquery.com/jquery.ajax/

此外,这可能是阅读有关 jQuery 和 AJAX 的更多信息的好页面,https://learn.jquery.com/ajax/jquery-ajax-methods/ .

关于php - 尽管我可以在 firebug post、html 和响应选项卡中看到 $_POST 数据,但 $_POST 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34828640/

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