SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: Javascript Object Help
-
Dec 13, 2009, 12:27 #1
- 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; }, }
Code:var result = pollService.dataStore.getPollByCode('sb1000'); var test = result['title'];
Follow Me On Twitter: BryceRay
-
Dec 13, 2009, 13:21 #2
- 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;
Code:var request = pollService.api.connect();
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.
-
Dec 13, 2009, 13:39 #3
- 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
-
Dec 13, 2009, 14:52 #4
- 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)" );
<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>
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
-
Dec 13, 2009, 16:01 #5
- 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
-
Dec 13, 2009, 17:30 #6
- 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
-
Dec 14, 2009, 14:09 #7
- 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