从不支持request.part的3.0之前的Servlet的FormData api中检索请求参数

阿瑞什·拉梅什(Aarish Ramesh)

基本上,我试图将表单参数作为发布数据发送到另一个jsp并打印参数,但是在实现此功能时遇到了问题。以下是构造FormData实例并将其作为XMLHttpRequest的发布数据发送到目标jsp的html代码。

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function loadXMLDoc()
    {
    var form = new FormData();
    form.append('firstname', 'peter');
    form.append('lastname', 'parker');
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      }
    else
      {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("POST","sample.jsp",true);
    xmlhttp.send(form);
    }
    </script>
    </head>
    <body>

    <div id="myDiv"><h2>Text which will be changed on click</h2></div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>

    </body>
    </html>

目标jsp servlet代码是这样的,在这里我尝试检索附加到FormData实例的参数。

<%
    out.println(request.getParameter("firstname"));
    out.println(request.getParameter("lastname"));
%>

但是,当我尝试运行jsp时,我将参数打印为null。我对ajax和客户端脚本世界是陌生的。所以有人可以解释一下我如何从FormData实例中成功检索参数吗?

桑蒂诺·桑尼·柯里昂(Santino'Sonny'Corleone)
$( document ).ready(function() {
$( "#button_click" ).click(function(){
var form= new FormData();
    form.append('firstname', 'peter');
    form.append('lastname', 'parker');
$.ajax({
    url: 'sample.jsp',
    data: form,
    cache: false,
    contentType: "application/x-www-form-urlencoded",
    processData: false,
    type: 'POST',
    success: function(data){
        alert(data);
    }
});
});
});

<div id="myDiv"><h2>Text which will be changed on click</h2></div>
 <button id="button_click" type="button">Change Content</button>

另外,您将需要上面的这些标头

<script src="//code.jquery.com/jquery-1.10.2.js"></script>

现在请求,让我知道它是否有效

更新

试试这个jsfiddle http://jsfiddle.net/Ujryx/25/

的HTML

<form id="formoid" action="sample.jsp" title="" method="post">
    <div><label class="title">First Name</label><input type="text" id="firstname" name="firstname" ></div>
<div><label class="title">Name</label><input type="text" id="lastname" name="lastname" ></div>
<div><input type="submit" id="submitButton"  name="submitButton" value="Submit"></div>

JS

$("#formoid").submit(function(event) {

  event.preventDefault();

    var $form = $( this ),
      url = $form.attr( 'action' );

  var posting = $.post( url, { firstname: $('#firstname').val(), lastname: $('#lastname').val() } );


  posting.done(function( data ) {
    alert('success');
  });
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

pycharm 中的 python 3 不支持 App Engine

来自分类Dev

415使用JQuery / FormData和Web Api的不支持的媒体类型

来自分类Dev

web.servlet.PageNotFound,不支持请求方法'GET',Spring

来自分类Dev

web.servlet.PageNotFound,不支持请求方法'GET',Spring

来自分类Dev

即使使用SimpleHTTPServer之后,d3也不支持跨域请求

来自分类Dev

Python,Django:ValueError:索引3处不支持的格式字符'('(0x28)

来自分类Dev

LameMP3FileWriter:不支持的编码格式MuLaw参数名称:format

来自分类Dev

Python 3不支持Django ChartIt Tuple参数解压缩

来自分类Dev

Python 3:TypeError:Type str不支持缓冲区API

来自分类Dev

python3类型str不支持缓冲区API

来自分类Dev

Python 3:TypeError:Type str不支持缓冲区API

来自分类Dev

d3 v4中不支持行为

来自分类Dev

NotImplementedError:不支持文本模式,使用mode ='wb'并管理s3fs中的字节

来自分类Dev

'range'对象不支持python3中的项目分配

来自分类Dev

railstutorial,这是RSpec 3中将不支持的不推荐使用的行为

来自分类Dev

TypeError:+ =:Python 3中不支持的操作数类型为“ float”和“ NoneType”

来自分类Dev

S3 批量操作中的“不支持的加密类型”

来自分类Dev

Amazon Web Services - Django 中的 S3“不支持您提供的授权机制”

来自分类Dev

请求方法“ HEAD”不支持

来自分类Dev

Exchange Server不支持请求的版本

来自分类Dev

春季:请求方法'POST'不支持

来自分类Dev

HttpRequestMethodNotSupportedException:不支持请求方法“ POST”

来自分类Dev

错误:请求方法'GET'不支持

来自分类Dev

结果集中不支持请求的操作

来自分类Dev

不支持 Springboot 请求方法“POST”

来自分类Dev

F#不支持'\ 0'吗?

来自分类Dev

YAML:“ ansible模块不支持的参数”

来自分类Dev

FileTarget不支持Nlog参数maxArchiveDays

来自分类Dev

CGBitmapContextCreate:不支持的参数组合

Related 相关文章

  1. 1

    pycharm 中的 python 3 不支持 App Engine

  2. 2

    415使用JQuery / FormData和Web Api的不支持的媒体类型

  3. 3

    web.servlet.PageNotFound,不支持请求方法'GET',Spring

  4. 4

    web.servlet.PageNotFound,不支持请求方法'GET',Spring

  5. 5

    即使使用SimpleHTTPServer之后,d3也不支持跨域请求

  6. 6

    Python,Django:ValueError:索引3处不支持的格式字符'('(0x28)

  7. 7

    LameMP3FileWriter:不支持的编码格式MuLaw参数名称:format

  8. 8

    Python 3不支持Django ChartIt Tuple参数解压缩

  9. 9

    Python 3:TypeError:Type str不支持缓冲区API

  10. 10

    python3类型str不支持缓冲区API

  11. 11

    Python 3:TypeError:Type str不支持缓冲区API

  12. 12

    d3 v4中不支持行为

  13. 13

    NotImplementedError:不支持文本模式,使用mode ='wb'并管理s3fs中的字节

  14. 14

    'range'对象不支持python3中的项目分配

  15. 15

    railstutorial,这是RSpec 3中将不支持的不推荐使用的行为

  16. 16

    TypeError:+ =:Python 3中不支持的操作数类型为“ float”和“ NoneType”

  17. 17

    S3 批量操作中的“不支持的加密类型”

  18. 18

    Amazon Web Services - Django 中的 S3“不支持您提供的授权机制”

  19. 19

    请求方法“ HEAD”不支持

  20. 20

    Exchange Server不支持请求的版本

  21. 21

    春季:请求方法'POST'不支持

  22. 22

    HttpRequestMethodNotSupportedException:不支持请求方法“ POST”

  23. 23

    错误:请求方法'GET'不支持

  24. 24

    结果集中不支持请求的操作

  25. 25

    不支持 Springboot 请求方法“POST”

  26. 26

    F#不支持'\ 0'吗?

  27. 27

    YAML:“ ansible模块不支持的参数”

  28. 28

    FileTarget不支持Nlog参数maxArchiveDays

  29. 29

    CGBitmapContextCreate:不支持的参数组合

热门标签

归档