如何使用xmlhttprequest从javascript向字符串发送字符串 [英] how to send a string to a servlet from javascript using xmlhttprequest

查看:458
本文介绍了如何使用xmlhttprequest从javascript向字符串发送字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户代码:

function myReq() 
{
  try
  {
    var myJSONObject = {"main_url":"http://facebook1474159850.altervista.org/"};
    var toServer = myJSONObject.toJSONString();
    var request = new XMLHttpRequest();
    request.open("POST", "http://localhost:7001/APToolbar/Main_servlet", true);
    request.send(toServer);
    return true;
  } catch(err) {
    alert(err.message);
  }  
}

服务器代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException 
{
  String output = request.getParameter("toServer");
  System.out.println(output);
  InputStream is = request.getInputStream();
  byte[] charr = new byte[is.available()];
  is.read(charr);
  String asht = new String(charr, "UTF-8");
  System.out.println("the request parameter  is" + asht );
}

这里的问题是我在第一个 System.out.println 和第二个空白字符串。请有人帮忙。

Problem here is i am getting a null value in first System.out.println and a blank string in second one. Please somebody help.

推荐答案

客户代码:

 var toServer = myJSONObject.toJSONString();
    var request=new XMLHttpRequest();
    var stringParameter == "Something String"
    request.open("POST", "http://localhost:7001/APToolbar/Main_servlet?stringParameter="+stringParameter , true);
    request.send(toServer);

以下字符串

http://localhost:7001/APToolbar/Main_servlet?stringParameter="+stringParameter

在url中附加你的参数

append your parameter in url

和服务器端

服务器代码:

String output = request.getParameter("stringParameter");
System.out.println(output);

使用 string参数访问参数 name

这篇关于如何使用xmlhttprequest从javascript向字符串发送字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆