[英]When I send a POST request from a JavaScript file to a PHP file in the same application, the PHP script always reads it as a GET request. Why?
I have a dynamic form on one of my web pages.我的 web 页面之一上有一个动态表单。 The form stores data in the script file and updates it as the user adds more data from the form.表单将数据存储在脚本文件中,并在用户从表单中添加更多数据时对其进行更新。 This is the object that I create in JS:这是我在 JS 中创建的 object:
const survey = {
title: '',
createdBy: '',
description: '',
openingMsg: '',
thankyouMsg: '',
questions: [],
}
The questions array holds Question objects. questions 数组包含 Question 对象。 Here is the class:这是 class:
class Question {
constructor(id) {
this.id = id
}
questionText = ''
type = ''
choices = new Map()
}
I add an event listener to the submit button like this:我向提交按钮添加了一个事件侦听器,如下所示:
const saveSurveyBtn = document.getElementById('saveSurvey')
surveyForm.addEventListener('submit', saveSurvey)
and the saveSurvey function looks like this:和 saveSurvey function 看起来像这样:
function saveSurvey(e) {
e.preventDefault()
fetch('../../actions/save-survey.php', {
method: 'POST',
body: JSON.stringify(survey),
headers: {
"Content-type": "application/json;charset=UTF-8"
}
})
}
As you can see I'm trying to send the data to a local php script from JS.如您所见,我正在尝试将数据从 JS 发送到本地 php 脚本。 All is well until I get to the php script.一切都很好,直到我找到 php 脚本。 The problem is this;问题是这样的; regardless of the fact that in my fetch call I use method: POST , it always gets sent as a GET request.不管在我的 fetch 调用中我使用method: POST ,它总是作为 GET 请求发送。 Why?为什么? How can I fix this?我怎样才能解决这个问题?
OK, I can't tell you why my form data wasn't being posted and I have confirmed that it was being sent as a GET request.好的,我不能告诉你为什么我的表单数据没有被发布,我已经确认它是作为 GET 请求发送的。 But this is how I solved this problem.但这就是我解决这个问题的方法。
I added a hidden field in the form and changed the button of type "submit" to type "button".我在表单中添加了一个隐藏字段,并将“提交”类型的按钮更改为“按钮”类型。 I then grabbed that button via JS and set a click listener on it.然后我通过 JS 抓住了那个按钮并在它上面设置了一个点击监听器。 When the user presses the button I first assign my JSON serialized survey object to the hidden field.当用户按下按钮时,我首先将我的 JSON 序列化调查 object 分配给隐藏字段。 Then I manually submitted the form and it was sent via POST to the PHP script.然后我手动提交了表单,它通过 POST 发送到 PHP 脚本。 The form's method was always "post" but that didn't seem to matter initially.表单的方法始终是“发布”,但最初似乎并不重要。 I don't know why but here is my input in the form:我不知道为什么,但这是我在表格中的输入:
<input name="survey" id="survey" type="hidden" value="">
then I grab the button which looks like this:然后我抓住看起来像这样的按钮:
<button type="button" class="btn btn-success" id="saveSurvey">Save Survey</button>
notice it's no longer of type "submit".请注意,它不再是“提交”类型。 I then add a click event listener to it and call this function when clicked:然后我向它添加一个单击事件侦听器,并在单击时调用此 function:
function saveSurvey(e) {
document.getElementById('survey').value = JSON.stringify(survey)
surveyForm.submit()
}
This worked.这行得通。 I don't know if I fully answered my own question so if someone else comes along with a better answer I will adjust this post.我不知道我是否完全回答了我自己的问题,所以如果其他人提出更好的答案,我会调整这篇文章。 Thank you everyone for your help.感谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.