I couldn't get the POST value in servlet page?
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?
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:dat开发者_高级运维aRequestHeader,
data:JSON.stringify(dataRequestObject),
success:function(){ alert("Request Done");},
error:function(xhr,ajaxOptions){
alert(xhr.status + " :: " + xhr.statusText);
}
});
Thanks in advance.
You shouldn't send it as JSON string, but just as JS object. Change
data: JSON.stringify(dataRequestObject),
by
data: dataRequestObject,
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");
// ...
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'
or
url: 'serv'
as well.
Can you try this solution:
http://developer.yahoo.com/javascript/howto-proxy.html
精彩评论