Javascript 通过url发送参数时,XmlHttpRequest.send()方法返回null

Javascript 通过url发送参数时,XmlHttpRequest.send()方法返回null,javascript,ajax,servlets,Javascript,Ajax,Servlets,我有一个html页面,其中我编写了一个javascript for ajax调用 下面是我的代码 var dbParam = JSON.stringify({"name" :"success"}); xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { }

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

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

从您的代码片段中,我可以看到在发送“xmlhttp.send(“x=“+dParam)”时出现了输入错误。请确保您发送的变量正确,这里应该是“dbParam

如上所述设置内容类型。

我甚至尝试使用字符串“x=”+“hello”,但它仍然返回null
 xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");