javascript - 如何解决 xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded") 的错误?
问题描述
(我搜索了很多,我在 Stack Overflow 上找不到这个问题)。
现在我正致力于通过 AJAX JavaScript 将大量数据从 HTML 保存到数据库到 php。这是我的 JavaScript 代码:
function save()
{
var hist = document.getElementById("hist").value;
var mission = document.getElementById("mission").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200 )
{
UserAccountInfo = this.responseText;
alert(UserAccountInfo);
}
}
xmlhttp.open("POST","savecompany.php",true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("history="+hist+"&mission="mission);
}
代码崩溃
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
如果我评论了这一行,我会收到带有计划字符串和数据库保存计划字符串的警报!
这是我的 PHP 文件
<?php
require "conn.php";
$history= $_POST["history"];
$mission = $_POST["mission"];
$sql = " UPDATE company SET history ='$history' , mission='$mission' where id='1'";
mysqli_query($conn,$sql);
echo $history;
mysqli_close($conn);
?>
我的错误在哪里?
解决方案
看起来你打算打字xmlhttp
并最终写了xhttp
。该变量未定义。
推荐阅读
- python - 限制 Python 文件的读写
- vaadin - 展开 VerticalLayout 宽度以匹配 Grid 宽度
- c# - C# 一种在包中捆绑多个小文件并更改/删除/创建它们的正确且简单的方法,而无需每次都重新创建整个 Bundle
- git - What does an asterisk mean in git blame for IntelliJ IDEA?
- build - 无法使用 Yocto 安装 pysqlcipher3
- c++ - 在不同的函数中锁定/解锁 std::unique_lock
- django - 如何在 Dj Rest Auth 中禁用密码?
- sql - Ignore hash character in a query on sqlplus
- javascript - 编译失败 ./src/App.js Module not found
- pip - 通过 github 共享 Jupyter notebook 的环境