问题描述
我有索引路由,我有一个带有textarea字段的表单,我想在提交该表单时发送一个发布请求,而无需重新加载页面,所以我正在做:
document.querySelector('form').onsubmit = (event) => {
fetch("https://jsonplaceholder.typicode.com/posts",{
method: 'POST',body: JSON.stringify({
body: document.querySelector('#new_message').value
}),headers: {
"Content-type": "application/json; charset=UTF-8","X-CSRFToken": getCookie('csrftoken')
}
})
.then(response => response.json())
.then(result => {
// Print result
console.log(result);
});
event.preventDefault();
}
此代码可以正常工作。但是,如果要在我的索引localhost fetch("/")
中发出发布请求,则此操作无效。 console.log不会打印任何内容。另外,使用获取JSONPlaceholder的代码,它不会在我的服务器上返回任何POST请求。如果我只是更改"/"
的网址,那将根本行不通。有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)