Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Web Service request in extension

I'm currently working on a Google Map extension where i want to consume an XML output from a web service that i am attempting to call.  I'm not an expert in Javascript or jQuery (which i belive will help me collect the XML) so i'm learning as i go along.  

Does anyone have an example of an extension that they are doing something similar what they wouldn't mind sharing in order that i can gain the knowledge needed to implement it into my extension?

This is my code which currently fails in the pareseResult function on the 'length' flag

    Qva.LoadScript ("Extensions/Mouchel/GMapsRealTime/jquery-1.4.4.min.js", function () {

                    // For more information on doing XMLHR with jQuery, see these resources:

                    // http://net.tutsplus.com/tutorials/javascript-ajax/use-jquery-to-retrieve-data-from-an-xml-file/

                    // http://marcgrabanski.com/article/jquery-makes-parsing-xml-easy

                    $(document).ready(function() {

                      $.ajax({

                              type: "POST",

                              url: "http://localhost/CEOStatusWebService/CEOStatus.asmx/GetStatusForAllCEOs",

                              data: "{}",

                              contentType: "application/xml; charset=utf-8",

                              dataType: "xml",

                              success: ajaxCallSucceed,

                              failure: ajaxCallFailed

                              // success: function(msg) {

                              // alert( "Data Saved: " + msg.d );

                              // ServiceSucceeded(msg);

                                // $('#RSSContent').removeClass('loading');

                                // $('#RSSContent').html(msg.d);

                                // alert("got here");

                              // }

                              // failure: function(msg) {

                              // funcitonToCallWhenFailed(msg);

                              // }

                      });

                    });

          });

          function ajaxCallSucceed(response) {

            alert("in success message");

                              $('#divLoading').hide();

            var CEO = eval('(' + response.d + ')');

                              alert(response.d);

                              alert(CEO);

            pareseResult(CEO);

        }

          function ajaxCallFailed(error) {

                    alert("in failed message");

                    $('#divLoading').hide();

                    alert('error: ' + error);

                    $('#divResults').hide();

          }

          function pareseResult(CEO) {

                    var lists = '';

                    for (var i = 0; i < CEO.length; i++) {

                              if (lists == '') {

                                        lists = CEO.ProductName;

                              }

                              else {

                                        lists = lists + " <br />" + CEO.ProductName;

                              }

                    }

                    $('#divResults').html(lists);

                    $('#divResults').show();

          }

Thanks

Lewis

34 Replies
Brian_Munz
Employee
Employee

Ok, how about:

alert($(response).find("string").find("CEOStatus").find("CEO").length);

Not applicable
Author

Still returns zero, i'll carry on working on it monday.  Thanks for all your help so far...

Brian_Munz
Employee
Employee

Okay, this should definitely work.  I made a small working example on my laptop and tested this out.

Replace the $.ajax, $(document).ready, and parseResult function with this, and it should alert out the <Number> value from the xml.

$(document).ready(function() {

      $.get('ceo.xml', function(data){

              parseResult(data);

      });

});


function parseResult(data){

     $.each($(data).find("CEO"), function(){

          alert($(this).find("Number").text());

     });

}

Brian_Munz
Employee
Employee

Or if you wanted to still use the $.ajax function, you'd need to adjust it to this:

$.ajax({

                              type: "GET",

                              url: "ceo.xml",

                              success: function(data){

                                 parseResult(data)

                              }

                             

                      }) ;


Not applicable
Author

Brian

A couple of lines of amended code in the web service itself to allow script languages to call it and a change from XML to JSON and i've got data in the output object.

I'll play with the code to now try and grab some details out of the output data to see how far i can get

JSON output.png

Brian_Munz
Employee
Employee

Ok cool.

Having the data as JSON should make it a bit easier to parse through.

Brian Munz

Web Technology Lead

484.654.2196

qlik.com<http://www.qlik.com/>

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

Not applicable
Author

Brian

I'm now workign with a web service thats outputting the data in JSON format (as attached).  I appreciate its a big ask given all the help you have given but could you have a quick look at the file and give me a couple of pointers on how to parse the data?

Thanks

Lewis

P.S this will be 2 beers now!

Brian_Munz
Employee
Employee

Well it looks like the web service returns the data as a sort of JSON/XML hybrid.  The JSON is encapsulated in XML so doing a proper JSON ajax load like this doesn't work.

$.ajax({

                                   dataType: 'json',

                                   success: function(data) {

                                                  alert('worked');

                                   },

                                   url: 'json.txt',

                                   error: function(){

                                                  alert('fail');

                                   }

                    });

Still, you can still make it work this way I think (although did you ever try the XML solution I posted above?  Because I think I had it.)

Either way, I'll post in a little while with some code that might help.    

Brian_Munz
Employee
Employee

Does the JSON that is returned actually come back with the <string xmlns="http://tempuri.org/"> tags at the top and bottom?  If so, this is a problem because it's essentially causing JQuery to fail in parsing the json since those tags aren't valid JSON. 

If I remove those manually from the file you attached and run my code against it, it all works fine.

So if it's not returning true JSON and instead return JSON wrapped in XML, it kind of defeats its own purpose and you might want to try the XML solution I outlined above.

Brian_Munz
Employee
Employee

Okay, nevermind, I got it figured out with the <string> tags in there, you just have to do some tricky conversion.

So here's the code:

$.ajax({

          dataType: 'xml',

          success: function(data) {

                    parseResult($(data).find("string"));

          },

          url: 'json.txt',

});

function parseResult(data){

          var t = {};

          t = $.parseJSON($(data).text());

          $.each(t.Table, function(){

                    alert(this[0] + " " + this[1] + " " + this[2] + " " + this[3] + " " + this[4] + " " + this[5] + " " + this[6] + " " + this[7] + " ");

          });

}

By the time the code is passed into parseResult, I've already bypassed the <string> tags, but it's still technically, XML, so I use the jQuery .text() method to get the XML content (which is basically JSON) as text then use $.parseJSON to convert it to true JSON that javascript can parse.  After that, I just loop through the Table array and output the different values.