• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AJAX 'POST' xml request to server

 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Eric,
I have tried in sending POST request to a ValidateServlet. Now instead of passing parameter in queryString, i passed a parameter in req.send("id="+value) like method call. I did this much. but i want to send xml format data in POST. Can i do that.

Please help me in this case
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you'd just send the XML as the string parameter in req.send():

req.send("<root><node id='node1' >test</node><node id='node2'>text2</node></root>");

How you build the string is obviously up to you.

-Yuriy
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set the content type...
reqXML.setRequestHeader("Content-Type", "text/xml")

Eric
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric, I
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My mistake. That's why I go here, learn something new every day

-Yuriy
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,
I have done so for by use of POST request. The code is given below

function validate(xml) {
var url = "ajaxUpdate";//?id="+xml;

if (window.XMLHttpRequest) {// Non - IE browsers
req = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE browser
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "text/xml");
req.onreadystatechange = callback;
req.send("id="+xml);
}
As the code given, i am sending xml formatted string in req.send(..) where as parameter is id, i am getting null on server side. But in case i use same id parameter in very first statement
<b>var url = "ajaxUpdate";//?id="+xml;</b> by removing comments on id. So in this way i receive same value on server side. And i also set
req.send(null) in this case.

Is this right way for sending.

Second thing, if i received xml or any type of post data in this way, i could not take attribute in xml, as u know attribute, we need equals sign and as i am using = sign for parameter id,
so by writing like this :

var url = "ajaxUpdate?id=<message name='pawan'>one</message";
it means there are two parameters one is id and second is name, Is there any way to remove this problem.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do this, you use the query string to get the values


If you do this, you use the form to get the value



if you want to send xml back, it would be like:


Well I am off to bed, not sure if this shed any light on the matter.

Eric
[ July 18, 2005: Message edited by: Eric Pascarello ]
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric again,

Please also put light on this point,
posted July 18, 2005 10:42 PM
--------------------------------------------------------------------------------
If you do this, you use the query string to get the values

code:
--------------------------------------------------------------------------------

var url = "ajaxUpdate.jsp?id=asdf";reqXML.open("GET", url, true);reqXML.send(null)

--------------------------------------------------------------------------------



If you do this, you use the form to get the value

code:
--------------------------------------------------------------------------------

var url = "ajaxUpdate.jsp";reqXML.open("POST", url, true);reqXML.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); reqXML.send("id=asdf")

--------------------------------------------------------------------------------


********if you want to send xml back, it would be like:********

code:
--------------------------------------------------------------------------------

var url = "ajaxUpdate.jsp";reqXML.open("POST", url, true);reqXML.setRequestHeader("Content-Type", "text/xml")reqXML.send("<?xml version='1.0' encoding='UTF-8'?><entry><item>asdf</item></entry>");

How could we get this xml data on server end, as we use request.getParameter(..) for getting data, so what object we use on server side in our jsp for getting this xml string.

Thanks and Regards,
Pawan
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you post the xml document to the server, you need to use an input stream to retrieve it. I do not know how to do it in jsp, but .NET would look like:



Eric
[ July 19, 2005: Message edited by: Eric Pascarello ]
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once the request gets tot he server there's nothing special about it. In other words, there doesn't need to be any special processing because you are using XMLHttpRequest on the client side.

So you are able to process the request in your servlet just like any other POST operation.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[/qb]<hr></blockquote>
Without compiling/testing, it would look something like the following:

[ July 19, 2005: Message edited by: Eric Pascarello ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic