I could not get the POST value on the servlet page?

I could not get the POST value on the servlet page. my previous question related to this question. How to get data from ajax request on servlet page?

I need the value of dataRequestObject 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); } }); 

Thanks in advance.

0
javascript jquery servlets
May 20 '11 at 6:57
source share
2 answers

You should not send it as a JSON string, but as a JS object. Change

 data: JSON.stringify(dataRequestObject), 

by

 data: dataRequestObject, 

and access the values ​​in the servlet in the usual way with the keys present in the JS object

 String mark = request.getParameter("mark"); String subject = request.getParameter("subject"); String language = request.getParameter("language"); String author = request.getParameter("author"); // ... 



Please note that your servlet must run in the same domain, otherwise you will end up in the Same origin rule . If it really works in the same domain, I would not hard code the domain in JS code, since it makes your code completely unsportsmanlike. Therefore replace

 url: 'http://localhost:8090/SampleServlet1/serv' 

by

 url: '/SampleServlet1/serv' 

or

 url: 'serv' 

.

+3
May 20 '11 at 12:59
source share
β€” -
0
May 20 '11 at 7:03 AM
source share



All Articles