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
Not applicable
Author

right done that to pass across the data but unfortunately its returning an error saying date is undefined.

And yes the success functions were firing before which made me think i was nearly there with it...

again yes i put back the original function to make the URL call

This is what i know have

Qva.LoadScript ("/QvAjaxZfc/QvsViewClient.aspx?public=only&name=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

                    // $.get("http://localhost/CEOStatusWebService/CEOStatus.asmx/GetStatusForAllCEOs", function(data) {

                    // alert("test");

                    // alert($(data).length);

                    // });

          // });

                    $(document).ready(function() {

                    alert("test");

                       $.ajax({

                              type: "POST",

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

                              data: "{}",

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

                              dataType: "xml",

                              success: ajaxCallSucceed(data),

                              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");

                              alert($(response).length);

                              $('#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();

          }

Not applicable
Author

one thing i guess i should try is that the URL will return data to a script language.  I know if i invoke it through a browser i get data but would there be any restrictions through something like JavaScript and JQuery?  Someone mentioned SoapUI at work so i will take a look at that as well

Brian_Munz
Employee
Employee

A sure way to determine if the XML is being loaded at all would be to put the following at the beginning of your success function:

alert(response);

Some sort of message saying [Object XMLdocument] or something like it should pop up.

Thanks.

Not applicable
Author

Seem to be getting somewhere now - hopefully.  I've removed the passing of the data to the succeed function yet have kept in the alert(response) which now returns [Object] from within the succeed function.  The alert for the response length now returns 1, although i've just amended the data in the DBase so that 2 records are returned within the xml and its still giving me a count of 1.

I'm now getting the code to succefully work through to the Parese results function although i'm still not 100% if it actually has the XML data within the responjse object

Brian_Munz
Employee
Employee

Returning a value of only 1 actually makes sense because at that level of the XML, there is only one object. 

Try this and you might be good to go if you use this method to retrieve the data.  If it works, I can explain it better.

$.each($(response).find("CEO"), function (key) {

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

});

That should alert the <Number> value for each CEO in the XML.

Not applicable
Author

I've added that script into the parseResult function as below and the code will alert me with the 'in parse true' comment but i don't get the alert with the number returned. not that i want to appear thick but have i put the code in where you thought it should go?

function pareseResult(response) {

                    var lists = '';

                    alert("in parse"+$(response).length);

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

                                                  if (lists == '') {

                                                  alert("in parse true");

                                        $.each($(response).find("CEO"), function (key) {

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

                                        });

                                        //lists = $(response).ProductName;

                              }

                              else {

                                        alert("in parse false");

                                        lists = lists + " <br />" + $(response).ProductName;

                              }

                    }

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

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

                    alert("in parse exut");

          }

Brian_Munz
Employee
Employee

Oh i see.  As a test replace that entire for loop with the $.each code I mentioned (which is jquery's version of a for loop).

BTW, I'm assuming the XML still looks similar to what you posted.  I'm not sure where .ProductName would be coming from.

Not applicable
Author

not seeing the alert with the 'Number' value coming out.  This is the code now.  The ProductName came from the example code that i have been using so once i had things working i would have replaced it.

If i can get this working i'll defenitely owe you a beer! mind you the flights out to where ever your based might cost a bit!

function pareseResult(response) {

                    var lists = '';

                    alert("in parse: "+$(response).length);

                     // for (var i = 0; i < $(response).length; i++) {

                                                  // if (lists == '') {

                                                  // alert("in parse true");

                              $.each($(response).find("CEO"), function (key) {

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

                              });

                                        //lists = $(response).ProductName;

                              // }

                              // else {

                                        // alert("in parse false");

                                        // lists = lists + " <br />" + $(response).ProductName;

                              // }

                    //}

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

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

                    alert("in parse exit");

          }

Brian_Munz
Employee
Employee

Ha that could be expensive.

Hmm I feel we might be getting close.

Before the $.each loop try alerting:

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

Also try

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

And is the XML at all different from before?

Not applicable
Author

alert($(response).find("CEO").length);  This returned '0'

alert($(response).find("string").length); This returned '1'

The XML will be the same as i've not altered anything to do with the web service