【问题标题】:How to solve error at xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")? [closed]如何解决 xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded") 的错误? [关闭]
【发布时间】:2019-10-11 11:52:35
【问题描述】:

(我搜索了很多,在 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);
?>

我的错误在哪里?

【问题讨论】:

    标签: javascript ajax


    【解决方案1】:

    看起来您打算输入xmlhttp,结果却写了xhttp。该变量未定义。

    【讨论】:

    • 噢噢!!!!好的,我现在工作谢谢。但是我有一个新错误,如何发送这样的参数 xmlhttp.send("history="+hist+"&mission="mission);
    • 答案在这篇文章中:stackoverflow.com/questions/8064691/…
    • 这是 GET 而不是 POST !!
    • 好的放松哈哈。看起来您没有在最后一个变量上使用连接。 xmlhttp.send("history="+hist+"&amp;mission="mission); xmlhttp.send("history=" + hist + "&amp;mission=" + mission);
    • @EmanFateen:看起来您的问题已在此页面上得到解答。虽然您的助手可能愿意就小的补充问题提供帮助,但请 (1) 尝试自己研究新问题,然后 (2) 如有必要,提出新问题。
    猜你喜欢
    • 2019-02-04
    • 2013-11-10
    • 2019-10-26
    • 1970-01-01
    • 2019-05-24
    • 2018-12-27
    • 2018-05-12
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多