gpt4 book ai didi

php - 如何使用jquery提交将json字符串发送到php文件?

转载 作者:行者123 更新时间:2023-12-01 07:26:27 30 4
gpt4 key购买 nike

*嗨,我对在 Jquery 中使用 JSON 很陌生。我有一个使用 json 数据和 javascript 制作的 html 表单。 Json 字符串保存结果。现在我的问题是如何将此字符串发送到 php 文件。我正在使用 ajax 函数提交表单 *这是我的 json 字符串

 var jsonstr = JSON.stringify(result);

$.ajax({ type:"POST",
url: "ajax.php",
headers: {
'Content-Type': 'application/json',

success: function(){
alert('Test results submitted!');;

最佳答案

指定datadataType选项:

var jsonstr = JSON.stringify(result);

$.ajax({
type:"POST",
url: "ajax.php",
data:'json=' + jsonstr,
dataType:'json',
success: function(){
alert('Test results submitted!');
}
});

从 php 中,您可以使用以下方式获取 json 字符串:

$_POST['json'];

关于php - 如何使用jquery提交将json字符串发送到php文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9239155/

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