我无法在servlet页面中获取POST值? [英] I couldn't get the POST value in servlet page?
问题描述
我无法在servlet页面中获取POST值。我之前的问题与这个问题有关。如何从servlet页面的ajax请求中获取数据?
I couldn't get the POST value in servlet page. my previous question related to this question.How to get the data from ajax request in servlet page?
我的servlet页面中需要dataRequestObject值。
I need dataRequestObject value in my servlet page.
var dataRequestObject= {};
dataRequestObject= {mark:Mark,subject:English,language:C language,author:john};
var dataRequestHeader= {};
dataRequestHeader= {Username:uname,Password:pword,Domain:domain,WindowsUser:windowsuser};
$.ajax({
type:'POST',
url:'http://localhost:8090/SampleServlet1/serv', //calling servlet
cache:false,
headers:dataRequestHeader,
data:JSON.stringify(dataRequestObject),
success:function(){ alert("Request Done");},
error:function(xhr,ajaxOptions){
alert(xhr.status + " :: " + xhr.statusText);
}
});
提前致谢。
推荐答案
您不应该将其作为JSON字符串发送,而应该作为JS对象发送。更改
You shouldn't send it as JSON string, but just as JS object. Change
data: JSON.stringify(dataRequestObject),
by
data: dataRequestObject,
并按照通常的方式通过JS对象中的键访问servlet中的值
and access the values in the servlet the usual way by the keys as present in JS object
String mark = request.getParameter("mark");
String subject = request.getParameter("subject");
String language = request.getParameter("language");
String author = request.getParameter("author");
// ...
注意您的servlet需要在同一个域中运行,否则您将点击同源策略。如果它实际上在同一个域上运行,那么我不会在JS代码中对域进行硬编码,因为它会使您的代码完全不可移植。所以替换
Note that your servlet needs to run in the same domain, otherwise you hit the Same Origin Policy. If it's actually running on the same domain, then I would not hardcode the domain in the JS code since it makes your code totally unportable. So replace
url: 'http://localhost:8090/SampleServlet1/serv'
by
url: '/SampleServlet1/serv'
或
url: 'serv'
。
这篇关于我无法在servlet页面中获取POST值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!