Link to home
Start Free TrialLog in
Avatar of KeithMcElroy
KeithMcElroy

asked on

asp return xml object?

Can I call a CLASSIC asp page from Jquery ajax and have the asp page return a parsable xml dom
object (instead of string) so that I can consume it in jquery using xml parsing?

If so, how would I code the asp page to return the object

response.write  ???
Avatar of zc2
zc2
Flag of United States of America image

Say you have an XML object in the ASP code:
Dim objDoc
set objDoc = CreateObject("Msxml2.DOMDocument")

Open in new window

Then after you have done with creating/modifying it, you can send it to the client as text:
Response.ContentType = "text/xml"
Response.write objDoc.xml

Open in new window

or directly:
objDoc.save( Response )

Open in new window

Let's say you have a page called datapage.asp you post to via jquery.  Create an empty div with a class called result.  The data from the post to the external page will be placed in the empty div after the page loads.  

jquery is javascript.  Javascript runs after the page loads while your asp vb code runs before the page loads.  This means the two don't talk directly to each other and jquery and parse anything you present in asp vb because that will be rendered as  html, then the page loads and jquery takes over.


<div class="result"></div>


<script>
$.post('datapage.asp', function(data) {
  $('.result').html(data);
});
</script>
Another option is to build  up the xml in your asp code. Then render it, then have jquery do it's stuff.

<%
theXML=""
theXML=theXML&<color>red</color>
theXML=theXML&<color>white</color>
theXML=theXML&<color>blue</color>

response.write theXML
%>
<script>
//jquery code to parse xml
</script>
The simple answer is yes...

The next question is what would the ASP file look like.

First, it would NOT contain any JS, as indicated in some of the posts above, as this is XML to be consumed by JS/JQuery.

There are two ways of doing this. First, as mentioned by padas, build a string containing the data and response.write it.

Second, and more readable would be something like this:

<xml>
  <element>
     <sub-element>
     <item>Value</item>
     <sub-element>
  </element>
</xml>

Open in new window


... this could then have dynamic stuff added:

<xml>
  <element>
<%for i = 1 to 10%>
     <sub-element>
     <item><%=i%></item>
     <sub-element>
<%next%>
  </element>
</xml>

Open in new window


HTH

GH
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial