gpt4 book ai didi

javascript - ajax post请求后在php中解析json

转载 作者:行者123 更新时间:2023-12-01 02:29:28 26 4
gpt4 key购买 nike

假设有带有 ajax 请求的 javascript -

index.js-

var dataFeedback = $("#feedback_popup_message_body").val();
var jsonObj = JSON.stringify({dataFeedback: dataFeedback});
$.ajax({
url: "index.php",
type: 'POST',
data: jsonObj,
dataType: 'json',
success: function (data) {
console.log(data);
}
});

在服务器端,php 页面 -

index.php-

<?php
$myPostData = json_decode($_POST["dataFeedback"]);
$feedback = $myPostData["dataFeedback"];
echo $feedback;
?>

我尝试通过请求发送 json 对象,一旦在服务器端解析它,将其返回到客户端页面并记录其数据。

在上述概念中,它不记录数据值。

我在 Chrome > F12 > Networks > index.php > Response 中检查了它并发现了这个 -

<br />
<b>Notice</b>: Undefined index: dataFeedback in <b>...\index.php</b> on line <b>11</b><br />

如何让它记录从服务器返回的数据

更新:最终导致其工作的代码 -

index.js-

var dataFeedback = $("#feedback_popup_message_body").val();

$.ajax({
url: "bin/bll/suggestionSummary.php",
type: 'POST',
data: {dataFeedback: dataFeedback},
success: function (data) {

console.log(data);
}
});

index.php-

<?php
$myPostData = $_POST['dataFeedback'];
echo $myPostData;

最佳答案

如果你有简单的值(value),那么你为什么要使用 JSON.stringify?试试这个...

var dataFeedback =  $("#feedback_popup_message_body").val();
var jsonObj = JSON.stringify({dataFeedback:dataFeedback});
$.ajax({
url: "index.php",
type: 'POST',
data: { dataFeedback:dataFeedback },
success: function(data){
console.log(data);
}
});

现在您可以通过 $_POST['dataFeedback']; 直接获取值

关于javascript - ajax post请求后在php中解析json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24181508/

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