34 Replies Latest reply: Jul 23, 2014 9:52 AM by James Pike RSS

    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[i].ProductName;
                                    }
                                    else {
                                              lists = lists + " <br />" + CEO[i].ProductName;
                                    }
                          }
                          $('#divResults').html(lists);
                          $('#divResults').show();
                }
      
      

       

      Thanks

       

      Lewis

        • Web Service request in extension
          Brian Munz

          Hi Lewis,

          Could you please post the XML code that is returned when you access the page directly?

          Also, are you needing to send information to the server in order to get the AJAX back?  The data parameter is used to send data to the server, so it's a little strange that you'd be sending over empty brackets.

          http://api.jquery.com/jQuery.ajax/

           

          Have you tried using the $.get jQuery method instead? 

          Thanks

            • Re: Web Service request in extension

              Brian


              This is the contents of my returned XML.  In this instance there is only one set of info however in a live situation there could be a couple of hundred records.

               

              I don't need to send any data to the server but from the reading i had done in the cintax i was using it just needed an empty data param.

               

              I've not looked at the $getjquery method but i'll have a look

               

               

               

               

               

              - <string xmlns="http://tempuri.org/">
                        - <CEOStatus>
                                  - <CEO>
                                              <Number>WS6178</Number> 
                                              <Lat>51.4934183333</Lat> 
                                              <LatNS>N</LatNS> 
                                              <Long>-0.1469100000</Long> 
                                              <LongEW>W</LongEW> 
                                              <LastGPSUpdate>2024-07-23T08:28:32+01:00</LastGPSUpdate> 
                                              <IsActive>0</IsActive> 
                                              <LastStatusUpdate>2011-07-13T09:52:59.1+01:00</LastStatusUpdate> 
                                    </CEO>
                          </CEOStatus>
                </string>
              
              

               

               

              Thanks

               

              Lewis

                • Web Service request in extension
                  Brian Munz

                  try replacing the ajax call with:

                   

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

                       alert($(data).length);

                  });

                   

                  See if you get an alert box with a number besides 0 in it.

                    • Re: Web Service request in extension

                      Brian

                       

                      Do you mean like this?

                       

                       

                          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
                                          $.get("http://localhost/CEOStatusWebService/CEOStatus.asmx/GetStatusForAllCEOs", function(data) {
                                          alert("test");
                                          alert($(data).length);
                                          });
                                });
                      
                      

                       

                      If so then i'm not getting any alerts coming back

                       

                      thanks

                       

                      Lewis

                        • Re: Web Service request in extension

                          Brian

                           

                          If it helps this it the complete script...

                          • Web Service request in extension
                            Brian Munz

                            Actually, now that I look closer, I'm not sure you're including jQuery properly.  If you add an alert after the

                             

                            $(document).ready(function()

                             

                            line of code, does it pop up?  Are you properly adding the extension to the document itself by using

                            Qva.AddExtension(...

                             

                            Thanks

                              • Web Service request in extension
                                Brian Munz

                                Ok, got the full script.

                                As far as I know, you need to use the server path when including script files.  So the jquery load script

                                 

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

                                 

                                should maybe be changed to

                                 

                                Qva.LoadScript ("/QvAjaxZfc/QvsViewClient.aspx?public=only&name=Extensions/Mouchel/GMapsRealTime/jquery-1.4.4.min.js", function () {

                                 

                                I would change that and do the test I mentioned above in $(document).ready to test that jquery is being loaded at all.

                                  • Web Service request in extension

                                    Brian

                                     

                                    I've changed the path and added an alert after the(docuemtn).ready(function... which does fire.  Ive added a few more alerts into the following succeed functions and they also fire so it looks liek its making its way through the code...

                                     

                                    if i add alert($(data).length); into my succeed function i get an undefined error

                                     

                                    ta 

                                     

                                    Lewis

                                     

                                    p.s thanks for you time looking at this

                                      • Web Service request in extension
                                        Brian Munz

                                        No problem.

                                        Were the success functions firing before?  I'm assuming that you're using the $.ajax method again if the succeed functions are firing?  If so, you need to pass the successfully retrieved data into the function.  Right now you have:

                                        success: ajaxCallSucceed,

                                        it should be

                                        success: ajaxCallSucceed(data),

                                         

                                        which will pass the data into your function.  It looks like you call it response in that function, so the success alert would be

                                         

                                        alert($(response).length);

                                          • Re: Web Service request in extension

                                            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[i].ProductName;
                                                                          }
                                                                          else {
                                                                                    lists = lists + " <br />" + CEO[i].ProductName;
                                                                          }
                                                                }
                                                                $('#divResults').html(lists);
                                                                $('#divResults').show();
                                                      }
                                            
                                            
                                              • Re: Web Service request in extension

                                                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

                                                  • Web Service request in extension
                                                    Brian Munz

                                                    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.

                                                      • Re: Web Service request in extension

                                                        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

                                                          • Re: Web Service request in extension
                                                            Brian Munz

                                                            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.

                                                              • Re: Web Service request in extension

                                                                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)[i].ProductName;
                                                                                              }
                                                                                              else {
                                                                                                        alert("in parse false");
                                                                                                        lists = lists + " <br />" + $(response)[i].ProductName;
                                                                                              }
                                                                                    }
                                                                                    $('#divResults').html(lists);
                                                                                    $('#divResults').show();
                                                                                    alert("in parse exut");
                                                                          }
                                                                
                                                                
                                                                  • Web Service request in extension
                                                                    Brian Munz

                                                                    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.

                                                                      • Re: Web Service request in extension

                                                                        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)[i].ProductName;
                                                                                                      // }
                                                                                                      // else {
                                                                                                                // alert("in parse false");
                                                                                                                // lists = lists + " <br />" + $(response)[i].ProductName;
                                                                                                      // }
                                                                                            //}
                                                                                            $('#divResults').html(lists);
                                                                                            $('#divResults').show();
                                                                                            alert("in parse exit");
                                                                                  }
                                                                        
                                                                        
                                                                          • Re: Web Service request in extension
                                                                            Brian Munz

                                                                            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?

                                                                              • Re: Web Service request in extension

                                                                                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

                                                                                  • Re: Web Service request in extension
                                                                                    Brian Munz

                                                                                    Ok, how about:

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

                                                                                      • Re: Web Service request in extension

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

                                                                                          • Re: Web Service request in extension
                                                                                            Brian Munz

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

                                                                                                 });

                                                                                            }

                                                                                              • Web Service request in extension
                                                                                                Brian Munz

                                                                                                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)

                                                                                                                              }

                                                                                                                             

                                                                                                                      }) ;

                                                                                                 


                                                                                                  • Re: Web Service request in extension

                                                                                                    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

                                                                                                      • Re: Web Service request in extension
                                                                                                        Brian Munz

                                                                                                        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.

                                                                                                          • Re: Web Service request in extension

                                                                                                            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!

                                                                                                              • Web Service request in extension
                                                                                                                Brian Munz

                                                                                                                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.    

                                                                                                                  • Web Service request in extension
                                                                                                                    Brian Munz

                                                                                                                    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.

                                                                                                                      • Re: Web Service request in extension
                                                                                                                        Brian Munz

                                                                                                                        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.