【问题标题】:XmlHttpRequest.send() method is returning null when parameters are sent through url通过 url 发送参数时,XmlHttpRequest.send() 方法返回 null
【发布时间】:2018-02-01 09:45:52
【问题描述】:

我有一个 html 页面,我在其中编写了一个用于 ajax 调用的 javascript 以下是我的代码

var dbParam = JSON.stringify({"name" :"success"});
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
}
}
xmlhttp.open("POST", "MyServlet", true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.send("x="+dbParam);

MyServlet 是我的 servlet 的 url 模式

在 Servlet 中

String s=request.getParameter("x");
This is returning null

【问题讨论】:

    标签: javascript ajax servlets


    【解决方案1】:

    从您的代码 sn-p 中,我可以在发送 "xmlhttp.send("x="+dParam)" 时看到一个错字。只要确保您发送的变量正确,就在这里应该是“dbParam”。

    【讨论】:

    • 我什至尝试使用字符串 "x="+"hello" 仍然返回 null
    【解决方案2】:
     xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    

    如上设置内容类型。

    【讨论】:

      猜你喜欢
      • 2016-12-27
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 2017-06-05
      • 1970-01-01
      相关资源
      最近更新 更多