aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes AJAX 'POST' xml request to server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX Watch "AJAX New topic
Author

AJAX 'POST' xml request to server

Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

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
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
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
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Set the content type...
reqXML.setRequestHeader("Content-Type", "text/xml")

Eric
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Thanks Eric, I
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
My mistake. That's why I go here, learn something new every day

-Yuriy
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

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
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jun 12, 2005
Posts: 264

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
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
[/qb]<hr></blockquote>
Without compiling/testing, it would look something like the following:

[ July 19, 2005: Message edited by: Eric Pascarello ]

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AJAX 'POST' xml request to server