SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Object Help

    I'm working on my first javascript based project. I'm attempting to create a javascript object to connect to some web services. However, something is wrong with my code because it is not working. I suspect it has something to do with the way i'm creating the namespace or classes or with the way i'm calling the methods in the classes. I appreciate any help or advice you might be able to offer me.

    Code:
    pollService = {
    	styles: {},
    	images: {},
    	
    	config: {
    		defaultExpirationLength: 120
           },
    	util: {
    		stripslashes: function(str) {
    		    return (str+'').replace(/\\(.?)/g, function (s, n1) {
    		        switch (n1) {            case '\\':
    		                return '\\';
    		            case '0':
    		                return '\0';
    		            case '':                return '';
    		            default:
    		                return n1;
    		        }
    		    });
    		}
    	}
    }
    
    	pollService.api = {
    		 connect: function() {
    			 //create request object and open a synchrnous post request 
    			var request = new XMLHttpRequest();	
    			request.open("POST","http://myservice.svc",false);
    	
    			//For requests using basicHttpBinding (ie: soap)
    			request.setRequestHeader("Content-type","text/xml;charset=UTF-8");
    			request.setRequestHeader("SOAPAction", '"http://tempuri.org/IService1/GetPollByCode"');
    			request.timeout = this.defaultExpirationLength;
    			return request;
    		 }
    	}
    	
    	pollService.dataStore = {
    	   getPollByCode: function(code) {
    		   
    		  var xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' +
    					'<soapenv:Header/>' +
    					'<soapenv:Body>' +
    						'<tem:GetPollByCode>' +
    						'<tem:code>' + code + '</tem:code>' +
    						'</tem:GetPollByCode>' +
    					'</soapenv:Body>' +
    					'</soapenv:Envelope>';
    					
    		  var request = pollService.api.connect;
    		  request.send(xml);
    		  
    		  var poll = new Array();
    		  if(request.status == 200){
    			var xmlResponse = request.responseXML;
    			poll['title'] = xmlResponse.evaluate( "string(s:Envelope/s:Body/GetPollByCodeResponse/GetPollByCodeResult/a:Poll/a:title)" );
    			poll['pid'] = xmlResponse.evaluate( "string(s:Envelope/s:Body/GetPollByCodeResponse/GetPollByCodeResult/a:Poll/a:pid)" );
    		 }
    		  
    		  return poll;
    	   }, 
    	   getPollsByEvent: function(eventString) {
    		   var xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' +
    					'<soapenv:Header/>' +
    					'<soapenv:Body>' +
    						'<tem:GetPollByCode>' +
    						'<tem:code>' + code + '</tem:code>' +
    						'</tem:GetPollByCode>' +
    					'</soapenv:Body>' +
    					'</soapenv:Envelope>';
    					
    			var request = pollService.api.connect;
    			request.send(xml);
    			
    			var poll = new Array();
    			if(request.status == 200){
    				var xmlResponse = request.responseXML;
    				poll['title'] = xmlResponse.evaluate( "string(s:Envelope/s:Body/GetPollByCodeResponse/GetPollByCodeResult/a:Poll/a:title)" );
    				poll['pid'] = xmlResponse.evaluate( "string(s:Envelope/s:Body/GetPollByCodeResponse/GetPollByCodeResult/a:Poll/a:pid)" );
    		  	}
    		  
    		  return poll;
    	   },
    	}
    I then try to call one of the methods like this:
    Code:
    var result = pollService.dataStore.getPollByCode('sb1000');
    	var test = result['title'];
    Follow Me On Twitter: BryceRay

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think this is a mistake:
    Code:
    var request = pollService.api.connect;
    Should be:
    Code:
    var request = pollService.api.connect();
    Can't see what else could be the problem.

    You should try to debug this by yourself with Firebug. The console will tell you at what point your script is failing, which will make it easier to isolate the problem. It'll also allow you to inspect the results of the XHR request in the Net tab.

  3. #3
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow that was the problem. Thanks for your help. It always seems the simplest things are the hardest to see.
    Follow Me On Twitter: BryceRay

  4. #4
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the current method i'm using to extract xml data.
    Code:
    var xmlResponse = request.responseXML;
    				poll['title'] = xmlResponse.evaluate( "string(s:Envelope/s:Body/GetPollByCodeResponse/GetPollByCodeResult/a:Poll/a:title)" );
    				poll['pid'] = xmlResponse.evaluate( "string(s:Envelope/s:Body/GetPollByCodeResponse/GetPollByCodeResult/a:Poll/a:pid)" );
    This works for some cases where only one 'poll' is returned. However if multiple polls are returned the xml is going to look something like this:
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header />
    <s:Body>
    <GetPollsByEventResponse xmlns="http://tempuri.org/">
    <GetPollsByEventResult xmlns:a="http://schemas.datacontract.org/2004/07/pollService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:Poll>
    <a:pid>2</a:pid>
    <a:title>Sample Title</a:title>
    </a:Poll>
    <a:Poll>
    <a:pid>3</a:pid>
    <a:title>Sample Title 2</a:title>
    </a:Poll>
    </GetPollsByEventResult>
    </GetPollsByEventResponse>
    </s:Body>
    </s:Envelope>
    Whats the best way to input the values of this xml value into an array such as the one below keeping in mind that the number of polls is dynamic.
    array[0]['pid'], array[0]['title'], array[1]['pid'], array[1]['title']

    I want to implement a for loop somehow but I can't figure out how to count the total number of nodes so that the for loop is possible.
    I've also tried to figure out XPath expression but i'm having trouble figuring out exactly how it works.

    Any help would be appreciated.
    Follow Me On Twitter: BryceRay

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You'd probably be better off receiving this data as JSON or just simple text, as it seems you're receiving fairly simple information.

    Anyway, XPath isn't necessary (and IE doesn't support evaluate). responseXML allows you direct access to the DOM of your XML fragment:

    Code:
    var nodes = result.responseXML.getElementsByTagName('GetPollsByEventResponse');
    alert(nodes.length); //would alert 2 with your sample code
    This article might help: http://www.quirksmode.org/blog/archi...ax_respon.html

  6. #6
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll give that a try. I'm not entirely sure what functionality I have because i'm actually not developing for the web i'm developing for a internet connected tv. If it has this functionality then it should work fine.
    Follow Me On Twitter: BryceRay

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, whatever it is, it must be based on one of the major browsers' rendering engines, and they all support this.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •