有人可以看出为什么它显示错误 Maximum call stack size exceeded?为什么脚本不起作用?更新:我添加了不放置值“domain”的 php 代码,并更新了 JS 代码。
function addentrys() {
var nwentry = {};
el = document.getElementById('addname').value;
eldmn = document.getElementById('adddomain').value;
nwentry.name = el;
nwentry.domain = eldmn;
$.ajax({
type: 'POST',
url: 'api/domain',
dataType: 'json',
data: nwentry,
succes: function(data) {
alert('Ok')
}
})
}
PHP:
//ADD
$app->post('/domain', function () {
$jsonContents = file_get_contents('data/data.json');
$name = $_POST['name'];
$domain = $_POST['domain'];
$data = json_decode($jsonContents, true);
$last_item = end($data);
$last_item_id = $last_item['id'];
$data[] = array(
'name' => $name,
'domain' => $domain,
'id' => $last_item_id+1
);
$json = json_encode($data);
file_put_contents('data/data.json', $json);
return $this->response->withRedirect('../index.html');
});
最佳答案
您正在将控制对象而不是值传递给 ajax。试试下面的代码
function addentrys() {
var nwentry = {};
el = document.getElementById('addname')
nwentry.name = el.value;
var data = JSON.stringify(nwentry)
$.ajax({
type: 'POST',
url: 'api/domain',
dataType: 'json',
data: (nwentry),
succes: function(result) {
console.log(result)
}
})
}
关于javascript - Ajax 通过 POST 发送数据(JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50622627/