【问题标题】:javascript : having trouble passing form values to javascript and processing it to httprequestjavascript:将表单值传递给 javascript 并将其处理为 httprequest 时遇到问题
【发布时间】:2010-10-31 12:05:48
【问题描述】:

我有一个填充表单数据的 java servlet(类)。

  //encode string for special characters
  String encodedString = URLEncoder.encode(desc, "UTF-8");

  out_table.append("<form name=\"masinsert"+id+"\" method=\"GET\" action=\"MASInsert2\">\n");

    out_table.append("<input type=hidden id=\"partnumber"+id+"\" name=\"partnumber"+id+"\" value=\""+part+"\">\n");
                    out_table.append("<input type=hidden id=\"itemdescription"+id+"\" name=\"itemdescription"+id+"\" value=\""+encodedString+"\">\n");


  out_table.append("<tr><td colspan=2><input id=\"m"+id+"\" type=\"button\" onclick=\"masinsert('"+ id +"')\" value=\"Add\"></td></tr>");

如你所见,它使用不同的 id 循环,所以表单看起来像这样

<form name="masinser12" method="GET" action="MASInsert2">
<input type=hidden id="partnumber12" name="partnumber12" value="9999">
<input type=hidden id="itemdescription12" name="itemdescription12" value="1. test description">
<input id="m12" type="button" onclick="masinsert('"+ id +"')" value="Add"> 

<form name="masinser13" method="GET" action="MASInsert2">
<input type=hidden id="partnumber13" name="partnumber13" value="2222">
<input type=hidden id="itemdescription13" name="itemdescription13" value="2. test description">
<input id="m12" type="button" onclick="masinsert('"+ id +"')" value="Add"> 

他们发布到生成表单的 html 头部的这个 javascript

function masinsert(id)
{
 var currentTime=new Date();


 var button = document.getElementById("m"+id);
 button.onclick="";
 button.value="Inserting";

 var partnumber     = document.getElementById("partnumber"+id).value;
 var itemdescription    = document.getElementById("itemdescription"+id).value;


 function handleHttpResponse()
  {
   if (http.readyState == 4) {
    button.value="Item Added";
   }
  }


 var http = getHTTPObject(); // We create the HTTP Object
 var tempUrl = "\MASInsert2";
 tempUrl +=  "?partnumber="+partnumber+"&"+"itemdescription="+itemdescription+"&"+"itemshortdescription="+itemdescription.substring(0,37)
alert(tempUrl);
 http.open("GET", tempUrl, true);
 http.onreadystatechange = handleHttpResponse;
 http.send(null);   
    }

function getHTTPObject(){
 var xmlhttp;
 /*@cc_on
 @if (@_jscript_version >= 5)
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e){
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }catch (E) {
      xmlhttp = false;
      }
  }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
 }

然后 javascript 转过来并发布到这个 java 文件,该文件使用 sn-p 将数据插入数据库

String partnumber = request.getParameter( "partnumber");
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String itemdescription = URLDecoder.decode(request.getParameter( "itemdescription"), "UTF-8");
        String itemshortdescription = request.getParameter( "itemshortdescription");
out.println(itemimport(partnumber,itemdescription, itemshortdescription));
} 

{ private String itemimport (String partnumber, String itemdescription, String itemshortdescription){ 
---
---
}

我决定缩短参数的数量以提高可读性。我遇到的主要问题是 URLDecoding 和 URLEncoding 如何正确传输以插入到 Java PreparedStatement 中。我已经缩小了 javascript 中某个地方的问题,因为当我创建一个简单的 html 表单,其中的变量填充了 URLEncoded 值时,它插入得很好。但是,当我通过 javascript 运行它时,出现 Null 错误

有没有更好的方法来做到这一点,或者有什么想法为什么价值观没有通过?在对要发布或获取的字符串进行编码时,javascript 是否需要一些解决方法?

提前致谢。

更新: 我刚刚在到达 request.getParamter 上做了一个 System.out.println,脚本停在 String itemdescription = URLDecoder.decode(request.getParameter("itemdescription"), "UTF-8"); 所以它显然有问题 URLDecoder.decode(request.getParameter("itemdescription"),"UTF-8")

【问题讨论】:

    标签: java javascript servlets


    【解决方案1】:

    以下是一些可以尝试的方法: 在您的 JavaScript 中,使用 encodeURIComponent 对您的 URL 参数进行编码:

    tempUrl +=  "?partnumber="+encodeURIComponent(partnumber)+"&"+"itemdescription="+encodeURIComponent(itemdescription)+"&"+"itemshortdescription="+encodeURIComponent(itemdescription.substring(0,37))
    

    接下来,您可能需要使用 setCharacterEncoding 设置请求的编码

    最后,我想如果你调用 request.getParameter,它会为你做 URLDecoding。

    【讨论】:

      【解决方案2】:

      看起来你应该在你的 javascript 中对你的请求参数值进行 url 编码

      tempUrl += "?partnumber="+partnumber+"&"+"itemdescription="+escape(itemdescription)+"&"+"itemshortdescription="+escape(itemdescription.substring(0,37))

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2019-11-16
      相关资源
      最近更新 更多