在发布这个问题之前,我已经搜索了我的问题,但没有找到解决方案。 我需要将 json 字符串发送到 php 文件,但无法这样做,有人可以帮助解决我的以下问题:我是 php 和 jquery 的新手,正在苦苦挣扎,需要您的合作。
我有一个函数可以捕获文本文件中的数据:
function updateVal() {
var node_list = document.getElementsByTagName('input');
var c = 0;
var fieldName = [];
var fieldText = []
var ID = [];
for (var i = 0; i < node_list.length; i++) {
var node = node_list[i];
if (node.getAttribute('type') == 'text') {
fieldName[c] = node.name;
fieldText[c] = node.value;
ID[c] = node.id;
c++;
}
}
var postData = {
fieldName: fieldName,
fieldText: fieldText,
ID: ID
};
var dataString = JSON.stringify(postData);
console.log(JSON.stringify(postData));
$.ajax({
type: "POST",
dataType: "json",
url: "update.php",
data: {myData:postData}
})
//return JSON.stringify(postData);
}
我的 update.php 是这样的:
<?php
$json = $_POST['json'];
$result = json_decode($json);
echo $result;
echo $_POST['myData']);?>
加载时:我收到以下错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
此外,我不确定是否将数据发送到 php。 请专家验证一下。
最佳答案
500(内部服务器错误)表示服务器端出现问题。因此,请检查 apache 错误日志以获取更多详细信息
你可以在这里找到 apache 日志 /var/log/apache2/
关于javascript - 使用 AJAX 将数据发送到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251197/