Ajax 没有将正确的数据传递给响应 php 页面上的 $_POST
Ajax not passing correct data to $_POST on responding php page
我有一个 ajax 函数:
$('#addSiteButton').on('click',function() {
let addSiteOption = $('#enterNewWebsiteLink').val();
const form = new FormData();
form.append('addSiteOption', addSiteOption);
$.ajax({
url: "includes/submit_site.php",
data: { 'data': form },
method: "POST",
contentType: false,
processData: false,
enctype: false,
cache: false,
datatype: "text",
success: function (response, data) {
}
});
我的 PHP 文件如下所示:
if (isset($_POST['data'])) {
$websitelink = $_POST['addSiteOption'];
}
我的 HTML 看起来像这样:
<input type="url" form="addsite" class="enterNewWebsiteLink"
id="enterNewWebsiteLink"
name="enterwebsitelink"
placeholder="Enter A New Website">
我得到的错误是:Warning: Undefined array key "data"
我试过输出变量 addSiteOption
并且它正确输出了 URL。
令人沮丧的是,我已经 checking/copying 这与我之前所做的一个项目相对应,我已经在其中工作了,现在我无法区分它们。
我从服务器收到该页面的 200 响应。
您不能序列化 FormData
,并且 processData: false
告诉 jQuery 不要尝试序列化 data:
选项。
您应该将 FormData
作为 data:
选项传递。然后 FormData
的键将成为 $_POST
键。
JS:
$('#addSiteButton').on('click',function() {
let addSiteOption = $('#enterNewWebsiteLink').val();
const form = new FormData();
form.append('addSiteOption', addSiteOption);
$.ajax({
url: "includes/submit_site.php",
data: form,
method: "POST",
contentType: false,
processData: false,
enctype: false,
cache: false,
dataType: "text",
success: function (response, data) {
}
});
PHP:
if (isset($_POST['addSiteOption'])) {
$websitelink = $_POST['addSiteOption'];
}
你也有错别字:datatype:
应该是dataType:
我有一个 ajax 函数:
$('#addSiteButton').on('click',function() {
let addSiteOption = $('#enterNewWebsiteLink').val();
const form = new FormData();
form.append('addSiteOption', addSiteOption);
$.ajax({
url: "includes/submit_site.php",
data: { 'data': form },
method: "POST",
contentType: false,
processData: false,
enctype: false,
cache: false,
datatype: "text",
success: function (response, data) {
}
});
我的 PHP 文件如下所示:
if (isset($_POST['data'])) {
$websitelink = $_POST['addSiteOption'];
}
我的 HTML 看起来像这样:
<input type="url" form="addsite" class="enterNewWebsiteLink"
id="enterNewWebsiteLink"
name="enterwebsitelink"
placeholder="Enter A New Website">
我得到的错误是:Warning: Undefined array key "data"
我试过输出变量 addSiteOption
并且它正确输出了 URL。
令人沮丧的是,我已经 checking/copying 这与我之前所做的一个项目相对应,我已经在其中工作了,现在我无法区分它们。
我从服务器收到该页面的 200 响应。
您不能序列化 FormData
,并且 processData: false
告诉 jQuery 不要尝试序列化 data:
选项。
您应该将 FormData
作为 data:
选项传递。然后 FormData
的键将成为 $_POST
键。
JS:
$('#addSiteButton').on('click',function() {
let addSiteOption = $('#enterNewWebsiteLink').val();
const form = new FormData();
form.append('addSiteOption', addSiteOption);
$.ajax({
url: "includes/submit_site.php",
data: form,
method: "POST",
contentType: false,
processData: false,
enctype: false,
cache: false,
dataType: "text",
success: function (response, data) {
}
});
PHP:
if (isset($_POST['addSiteOption'])) {
$websitelink = $_POST['addSiteOption'];
}
你也有错别字:datatype:
应该是dataType: