首页 > 解决方案 > 如何解决 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);
?>

我的错误在哪里?

标签: javascriptajax

解决方案


看起来你打算打字xmlhttp并最终写了xhttp。该变量未定义。


推荐阅读