【发布时间】:2021-12-21 15:59:38
【问题描述】:
我正在尝试按照this 指南在用户更改另一个字段时更新表单字段。
我已经正确设置了我的 FormTypes,但是我在 没有 JQuery 的情况下在 Ajax 中提交表单时遇到了问题。
我有 2 个选择:
const blockchain = document.getElementById('strategy_farming_blockchain');
const dapp = document.getElementById('strategy_farming_dapp');
const csrf = document.getElementById('strategy_farming__token');
blockchain 字段应该更新dapp 字段。
如果我提交整个表单,它就可以工作:
blockchain.addEventListener('change', function () {
const form = this.closest('form');
const method = form.method;
const url = form.action;
var request = new XMLHttpRequest();
request.open(method, url, true);
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
request.onload = function () {
if (this.status >= 200 && this.status < 400) {
//Success
const html = new DOMParser().parseFromString(this.response, 'text/html');
dapp.innerHTML = html.querySelector('#strategy_farming_dapp').innerHTML;
} else {
//Error from server
console.log('Server error');
}
};
request.onerror = function () {
//Connection error
console.log('Connection error');
};
request.send(new FormData(form));
});
但我不应该提交整个表单,我应该只提交 blockchain 值
我尝试了很多东西,比如
var formdata = new FormData(form);
formdata.delete(dapp.name);
request.send(formdata);
// It's working for a new entity, but if I'm editing one, it's not updating the dapp field...
或
var formdata = new FormData();
formdata.append(this.name, this.value);
formdata.append(csrf.name, csrf.value);
request.send(formdata);
// It's working in a NEW action, but not in an EDIT action...
或
var data = {};
data[this.name] = this.value;
request.send(data);
//or
request.send(JSON.stringify(data));
//If I dump($request->request) in the controller, it seems like there's no data...
//Or the request isn't parsed correctly, or there's something missing ?
我也试过encodeURIComponent...
我没有想法......有什么想法吗?谢谢!
【问题讨论】:
-
请参阅 here 关于 CSRF 令牌。
-
@Barmar 谢谢。我修复了“CSRF 令牌无效”错误。如果我在一个 NEW 操作中它正在工作,但如果它是一个 EDIT 操作,它不会更新 dapp 字段(我检查了响应并且没有 dapp 字段的新选项......)。我会编辑帖子,但它没有关闭...
-
如果你只发送
blockchainerlement,它怎么知道应该是什么动作? -
我也尝试发送整个表单并仅删除带有
formdata.delete(dapp.name);的 dapp 字段,它是一样的 -
我不知道
dapp是什么,或者你为什么需要删除它。
标签: javascript ajax symfony