javascript 如何使用 XMLHttpRequest.send(string) 中的信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12787291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:07:20  来源:igfitidea点击:

How to use the information in XMLHttpRequest.send(string)

javajavascriptajax

提问by Jeff Engebretsen

I'm sending a POST request with a string in the .send()part which is "id=jeff&command=test".

我正在发送一个带有字符串的 POST 请求,.send()其中的"id=jeff&command=test".

How do I parse and use these values in Java via the HttpServletRequestobject?

如何通过HttpServletRequest对象在 Java 中解析和使用这些值?

I've tried changing the content type to text/htmland application/x-www-form-urlencoded. My java server is embedded jetty btw.

我试过将内容类型更改为text/htmlapplication/x-www-form-urlencoded。我的 Java 服务器是嵌入式码头顺便说一句。

var text = form.command.value;
var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()
    {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
            xmlhttp.responseText
          }
        }

    xmlhttp.open("POST", 'go', true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("id=jeff&command=" + text);

here's my Java code

这是我的 Java 代码

public class GoHandler extends HttpServlet 
{
Commands c = Commands.getInstance();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
{
    BufferedReader reader = request.getReader();
    String str = "";
    while ((str = reader.readLine()) != null)
    {
        System.out.println(str);
    }

    String p = request.getParameter("id");
    String input = request.getParameter("command"); 
    System.out.print(p);

here is my output when I make the request from a browser

这是我从浏览器发出请求时的输出

id=jeff&command=test (this is from the buffered reader)

id=jeff&command=test(来自缓冲阅读器)

null (this is from my String p which should be the id)

null(这来自我的字符串 p,它应该是 id)

here's Chrome's toolkit thing..

这是 Chrome 的工具包东西..

Request URL:http://localhost:8080/go
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:20
Content-Type:application/xml
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko)     Chrome/22.0.1229.79 Safari/537.4
Request Payload
id=jeff&command=test
Response Headersview source
Content-Length:66
Content-Type:application/json;charset=utf-8
Server:Jetty(8.1.7.v20120910)

my response from the server

我的服务器回复

{"flavor":"You are in the goHandler"}

采纳答案by devsathish

You can get it as buffered reader. This will give full string. The you can parse it manually. If your separator is "\n" then,

您可以将其作为缓冲阅读器获取。这将给出完整的字符串。您可以手动解析它。如果你的分隔符是 "\n" 那么,

BufferedReader reader = request.getReader();
while ((str = reader.readLine()) != null)
  jb.append(str);

Edit:

编辑:

Can you try one/both of these http headers?! Here is your updated code.

您可以尝试其中一个/两个 http 标头吗?!这是您更新的代码。

var text = form.command.value;
var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange=function()
{
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        alert(xmlhttp.responseText);
      }
    }
var params = "id=jeff&command=" + encodeURIComponent(text);
xmlhttp.open("POST", "go", true);
xmlhttp.send(params);

Using encodeURIComponent() to convert the URI into valid ASCII.

使用 encodeURIComponent() 将 URI 转换为有效的 ASCII。

Updated the JavsScript since the default content-type is "application/x-www-form-urlencoded" so removed it. Now try to access all your parameters in the servlet.

更新了 JavsScript,因为默认的内容类型是“application/x-www-form-urlencoded”,因此将其删除。现在尝试访问 servlet 中的所有参数。

回答by Vijay Vishwakarma

"That works. But is there no built in way of getting these values?"

“那行得通。但是没有内置的方式来获取这些值吗?”

Don't read input stream to get parameter because once you read input stream you reached at the end of input stream and native method getParameter()does not reset input stream that's why it returns null. use getParameter()directly to get values.

不要读取输入流来获取参数,因为一旦读取了输入流,您就会到达输入流的末尾,而本机方法getParameter()不会重置输入流,这就是它返回 null 的原因。getParameter()直接使用获取值。

Use either InputStreamor getParameter()don't use both

使用InputStreamgetParameter()不使用两者

@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException
{`    `

String p=request.getParameter("id");
String input=request.getParameter("command");
System.out.println(p);