Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com�s entire library of video tutorials.
Let's start by looking at XMLHttpRequest. This is the heart of it all. It's a JavaScript function that is not terribly hard to use once you get the hang of it, but it does require you to understand a bit about programming and how handlers work. So, let's take a look at it. We didn't have to look that closely into the code for data sources and for using the built-in database, but here we are going to have to use some JavaScript code. Some parts are available for you in Dashcode, so it's just a matter of putting them together, but I'll show you the overall picture now as we are going into the world of code. And what I've done here is I've created a demonstration program that I can open a Dashcode and I'll show you what's in this code and then I'm going to open it in a browser and you'll see how it works. We're not here in the world of iPhone web apps yet. This is just a plain exercise in going out to the web to get data that's located there. And what this is, you can find this kind of sample code on the web in many cases, but this is a very stripped down version of using XMLHttpRequest. And what it does is it's a script, this is JavaScript and what I've got up here is I create the request object here using the new operator and I'm setting an XML document to know. I'll be using that later but I don't even need it right now, so I'll just take that out. And what I do is I've got this XMLHttpRequest object in the variable XML Request and what I want to do is to set a handler. There are a number of handlers that you can set, but here, you see I've got the object here and I'm setting the handler, which is the onload property and I'm setting it to be a function. And this is the only part that's a little bit complicated if you're not used to it. I'm setting it to be a function and here, here is the body of the function. Normally when you declare a function, you give it a name and parameters. Here we just say, this is going to be a function. It doesn't matter what the name and parameters are because it's just used here. So, I'm creating it here and what I'm looking at is when this is called, then I'm going to come back to this object and check the Status property. And the only value that I care about is 200, because 200 means that the request has completed successfully. If it has not completed successfully, then I really don't care. There is an Else statement here that alerts people that there's been a problem fetching the data and it returns to Status in an alert. But all I really care about is that it's 200 and it should always be 200 because the onload handler is only called when the request has been loaded successfully. So, if it is not 200 and onload is called, something very strange is happening. So, it should always be the case, but when it's loaded, I've intercepted the data and as you'll see, I'll be able to do something here with the data. Right now, all I'm doing is I'm putting up an alert with the text in it that I've got from the XML Request. Now, I've created the XML Request up here. I've set the onload handler, which will fire when the data is loaded. But I haven't actually done anything. This is executable code here. I have to open the request. I'm going to use Get. You could also use Post. And this is the URL that I'm going to. And then I have to send the request. Up here, this is just I'm creating the variable and this is initialization. These 2 lines of code are executable. And so what will happen here is I will open it. I will send it and then the XML Request itself will watch to see when it's loaded and when it's loaded, it will come down here and put the response text up in an alert. It's as simple as that. The main thing if you're not used to using handlers like this is, what you would normally do in older forms of programming, is you would open it and send it and then you would test to see if it had completed properly. We get rid of If statements using handlers because we just set the whole thing up, set the parameters, put whatever handlers you want in there, and there could be other handlers there. You put the handlers there and then whatever we have declared will happen when the appropriate handler fires. So, it's not a matter of testing to see what the environment is. It's a matter of setting up the entire environment and then letting JavaScript and the browser take care of firing the appropriate handler at that time. We're not writing If statements anymore. So, this is the basic structure of the code, the JavaScript code in a browser and if I just double-click here and launch it into Safari, you'll see there's nothing on the page, but here is the alert from here where I'm returning the response text that I got and it happens to be XML but it could be any text.
| Course: | Developing iPhone Web Apps |
| Author: | Jesse Feiler |
| SKU: | 34075 |
| ISBN: | 1-935320-89-0 |
| Release Date: | 2009-12-31 |
| Duration: | 8 hrs / 103 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |