【发布时间】:2019-04-06 14:07:36
【问题描述】:
这是我的代码。我的POST 功能似乎有效。但是当我尝试从服务器console.log我的 POST 时,我得到的只是“null”。
我需要帮助是将我的表单正确地POST 发送到服务器。然后能够检索它并将其发布到我的控制台。
然后我必须将其发布到我的网站上,但我可以自己管理。
const myForm = document.getElementById("myForm");
myForm.addEventListener("submit", function(e) {
e.preventDefault();
const formData = new FormData(this);
formData.append("store", "vetlekw1");
fetch("http://folk.ntnu.no/oeivindk/imt1441/storage/add.php?", {
method: "post",
body: formData
})
.then(function(response) {
return response.text();
})
.then(function(text) {
console.log(text);
});
});
document.querySelector(".hent").addEventListener('click', e=>{
fetch('http://folk.ntnu.no/oeivindk/imt1441/storage/getAll.php?store=vetlekw1')
.then(res=>res.json())
.then(data=>{
console.log(data);
})
})
<section id="addContact">
<h1>add Contact</h1>
<form class="form" id="myForm" >
<label for="fornavn">Name</label>
<input type="text" id="fornavn"><br>
<label for="etternavn">Surname</label>
<input type="text" id="etternavn"><br/>
<label for="tlf">Tlf</label>
<input type="text" id="tlf"><br>
<button type="submit">add</button>
</form>
</section>
<br>
<button class="hent">Get</button>
【问题讨论】:
-
您是否查看了网络选项卡以了解更多请求详情?
-
您好!是的。这是我发布的时间:imgur.com/a/sq09CjT 这是我尝试从服务器获取它的时间:imgur.com/a/I8o1jOd
-
问题似乎出在服务器端。可以显示
getAll.php代码吗? -
另外,您能否确认数据是否已添加到数据库中?
-
目前我没有 getAll.php 代码。或者我无法访问它。这:imgur.com/a/sq09CjT 是我从服务器得到的响应。所以添加了一些东西。
标签: javascript forms post get