1

I have a jquery function which communicates with an ASP.NET web service which looks like this

$(document).ready(function() {
                         $.support.cors = true;
             $.ajax({
                 type: "GET",
                 url: "http://www.webservice.com/blahblah.asmx/blahb123",
                 data: "tnWsGuid=TEST1",
                 dataType: "text",
                 success: function(data, status, jqxhr) {
                     xmlString = data;
                     alert(xmlString);
                 },
                 error: function (request, status, error) {
                     alert(status);
                 }

                });
        });

Alert display's this:

<?xml version="1.0" encoding = "utf-8"?>
<string xmlns = "http://Walkthrough/XmlWebServices/">
{"approverName":"","emailAddress":"","companyName":"ABC","address":{"streetAddress1":"12 BlahBlah","streetAddress2":"","state":"ON","zipCode":"","country":"SO","phoneNumber":""},"tabledata:"[{"vendorPart":"AAAAA","partDescription":"N/A","price":"0.00","quantity":"28"},{"vendorPart":"BBBBBBB","partDescription":"N/A","price":"0.00","quantity":"3"},{"vendorPart":"CCCCCC","partDescription":"N/A","price":"0.00","quantity":"25"}]}
</string>

My dataType is now text. I know how to parse JSON however, now what I need to do is somehow access the JSON embedded in the XML envelope and turn it into a JSON object so I can use JQuery to parse it.

Here is what I have tried in the $.ajax function:

success: function(data, status, jqxhr) {
                     xmlString = data;
                     var jsondata = jQuery.parseJSON(xmlString.substr(xmlString.indexOf('{')));
                     alert(jsondata);
                 }

But was returned with an error of Invalid character which in the IE debugger look's like this

Any idea how I can access the data inside the xml envelope and turn it into a JSON object so I can parse it as JSON? I do not have the ability to change the web service so this must all be done within the web page.

2
  • Note that $.support.cors = true doesn't actually do anything unless the current browser supports CORS and jQuery incorrectly detects that it doesn't. Commented Apr 17, 2013 at 15:34
  • Is there a reason you have JSON inside XML and not just return the JSON directly? Commented Apr 17, 2013 at 15:34

3 Answers 3

1

You can do this:

success: function(data, status, jqxhr) {
    var xml = $($.parseXML(data)), // Parse the XML String to a Document, and Wrap jQuery
        json = xml.find("string").text(), // Get the text of the XML
        jsonObj = $.parseJSON(json); // Parse the JSON String
}

Or short notation

var jsonObj = $.parseJSON($($.parseXML(data)).find("string").text());

Live Fiddle: http://jsfiddle.net/59rQA/

Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me, tested it out and did jsonObj.companyName within the alert and it showed the right stuff! Marking this as the answer, thank you!
1

I think your substring is adding the closing tag for to the JSON data. How about:

xmlString = $.parseXML(xmlString);
var jsondata = $.parseJSON($(xmlString).children('string').text());

Comments

1

As your call is returning xml, you could use dataType:"xml" instead of "text".

You can then process the xml response:

var jsonData=$.parseJSON(data.find("string").text());

1 Comment

You forgot data needs to be $(data). Other than that, I prefer this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.