Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Development

How do I perform a web request with JavaScript?

Author Message
chrigil



Joined: 04 May 2005
Posts: 4
Location: London

Posted: Wed May 04, 2005 - 11:11 am    Post subject: How do I perform a web request with JavaScript? Reply with quote

I want to request a page and then simply display the result of that page.

For example if I call mypage.asp and this dynamic page renders the text 'Hello World', how would I firstly call the page from JavaScript and secondly how would I display the actual text?

Thanks in advance,

Chris
View user's profile Send private message Send e-mail Visit poster's website Widgets
sean



Joined: 22 Apr 2005
Posts: 67
Location: Duluth, MN

Posted: Wed May 04, 2005 - 4:45 pm    Post subject: Reply with quote

Code:
var req = new XMLHttpRequest();

url = "http://www.myserver.com/mypage.asp";
req.open("GET", url ,false);
req.send(null);
       
//req.responseText returns a string
response = req.responseText;

document.getElementById('content').innerHTML = response;
// or .innerText

Then in your html:

<div id="content"></div>

That should do what you need.
View user's profile Send private message Send e-mail Visit poster's website Widgets
fishnyc22



Joined: 01 May 2005
Posts: 4
Location: nyc

Posted: Wed May 04, 2005 - 5:34 pm    Post subject: Reply with quote

How does XMLHttpRequest(); work?

Can it send any from data through it or does it have to be structured in some type of XML format?

The reason I ask is that I was just looking for a similar answer for my post:
http://www.dashboardwidgets.com/forums/viewtopic.php?t=453

So I could do something like:

url = "http://www.myserver.com/mypage.asp";
req.open("POST", url ,false);
req.send(null);

and it would send my form data to the page? how does the response work? I don't have control over the page I am submitting to so I can't structure it in anyway to know I it was a success.

Thanks
View user's profile Send private message Send e-mail AIM Address Widgets
sean



Joined: 22 Apr 2005
Posts: 67
Location: Duluth, MN

Posted: Wed May 04, 2005 - 7:18 pm    Post subject: Reply with quote

Despite its name, XMLHttpRequest isn't only used for xml. It can send form data to a page, and retrieve the response, or it can just be used to pull the contents of a static web page into a javascript string.

I haven't played with it much, but what it has done for me, it's done well. (Just grabbing page content so far.)

But yes, you could do something like:

req = new XMLHttpRequest();
url = "http://www.myserver.com/mypage.asp";
req.open("POST", url ,false);
req.send("id=1&otherField=thisIsWhatImSubmitting");

if you want the response from the server as a string:

response = req.responseText;

There's more information about the object here:

http://developer.apple.com/internet/webcontent/xmlhttpreq.html

and if you search google, you should be able to find plenty of examples.
View user's profile Send private message Send e-mail Visit poster's website Widgets
chrigil



Joined: 04 May 2005
Posts: 4
Location: London

Posted: Thu May 05, 2005 - 8:32 am    Post subject: Reply with quote

Thanks guys it looks like the XMLHttpRequest object is just what I'm after. I knew it was possible I just didn't know what object to use.

Thanks again,

Chris.
View user's profile Send private message Send e-mail Visit poster's website Widgets
fishnyc22



Joined: 01 May 2005
Posts: 4
Location: nyc

Posted: Fri May 06, 2005 - 8:44 am    Post subject: Reply with quote

Hey sean,

If I'm using POST do I need to include the variables in the send function? Is there a limit to the lenght of the string? No right?

Thanks again for the help.
View user's profile Send private message Send e-mail AIM Address Widgets
avnit



Joined: 25 Apr 2005
Posts: 23

Posted: Fri May 06, 2005 - 10:57 am    Post subject: Reply with quote

Making a post request would look something like this:

Code:

req = new XMLHttpRequest()
url = "http://www.yourdomain/login.php"
req.open("POST", url ,false)
req.send("username=foo&password=bar")
x = req.responseText
View user's profile Send private message Send e-mail Visit poster's website Widgets
falcoboy7



Joined: 11 May 2005
Posts: 32

Posted: Fri Jun 24, 2005 - 12:58 am    Post subject: Reply with quote

I know it's all been handed to be on a platter, but I don't really understand how to use this. I want to make a widget to show the online players on a game. There is a .php page up that lists these players. So how do I go about grabbing that info and putting it on the widget? How do I grab only certain parts of text/in certain tags?

Thanks!
View user's profile Send private message Widgets
zzen



Joined: 21 Apr 2005
Posts: 43
Location: Czech Republic

Posted: Fri Jun 24, 2005 - 4:43 am    Post subject: Reply with quote

Chrigil,

you might actually try the code sean outlined above, you'll see what it does. Anyway, here is another try:

  1. You "call" the page via the XmlHttpRequest object the way it was indicated above. The XmlHttpRequest is somewhat like a web browser - you tell it which page you want - and XmlHttpRequest goes and retrieves it for you.
  2. You receive the page HTML source and can access it in the req.responseText variable. The contents of this variable is the same you will see if you go to the page in Safari and choose View Source from View menu.
  3. Now that you have the source of the page, you need to search through it for the data you need. There are various ways you can do this. You can use the responseText string object with functions like indexOf() and slice() or match() to extract certain parts of the code.
  4. You can observe me, Chris and sean discussing a similar task here. Of particular note, if your ASP page returns validating X(HT)ML then you might try using Chris's approach using responseXML and DOM methods.

Hope that helps.
View user's profile Send private message Send e-mail Widgets
kirkm



Joined: 30 Jun 2005
Posts: 2
Location: Ohio

Posted: Thu Jun 30, 2005 - 12:25 pm    Post subject: POST and Login Reply with quote

I have been struggling with using XMLHttpRequest() to log into a website. I can easily grab a website through "GET" on a static page and display it in my widget but when I try to log into a website using "POST" to grab information it will only take me to the log in page.

Here is the code I am using:

Code:

var req= new XMLHttpRequest();
url = "http://domain.com/login.php";
req.open("POST", url, false);
req.send(login=name&password=pass);
response = req.responseText;


If I open safari and just type in the address: http://domain.com/login.php?login=name&password=pass

no problem - it will log in. However, trying to log in using the XMLHttpRequest() in the widget will not log me in. I have seen some code posted about a login outside of the widget. I am trying to login (from the widget) then grab a couple of numbers on the home page (after the login) and display it in the widget (without going through safari). It seems like everything in the code should be right and I have allow Network access set to true in the plist file.

I don't know why it's not loggin me in. Any ideas?
View user's profile Send private message Send e-mail AIM Address Widgets
Mayhem



Joined: 18 May 2005
Posts: 63
Location: Stockholm, Sweden

Posted: Fri Jul 01, 2005 - 10:47 am    Post subject: Re: POST and Login Reply with quote

kirkm wrote:
Here is the code I am using:
Code:
req.send(login=name&password=pass);

The data sent by your code above will be quite unpredictable. You need something like:
Code:
req.send("login=" + name + "&password=" + pass);

_________________
Give me one more medicated peaceful moment
View user's profile Send private message Send e-mail Visit poster's website MSN MessengerWidgets
kirkm



Joined: 30 Jun 2005
Posts: 2
Location: Ohio

Posted: Fri Jul 01, 2005 - 9:20 pm    Post subject: Login/Pass Reply with quote

Thank you very much for the response javascript:emoticon('Smile') but I understand the code I submitted was not in correct form for the username and password. I will be grabbing the username / password from a form I have on the back of the widget and will be placed in the code.

However, If I can't get the widget to work with my username and password actually in the code, there's no way it will work with the username and passwords from the form.

I'm just a little confused as to why if I have placed my Login/Pass in the code:

Code:
// my login pass put directly in the code
req.send("login=mylogin&password=mypassword");

// my login / pass grabbed from the form on the back of the widget
req.send("login=" + name + "&password=" + pass);


Why the code will not grab the information on the page after the login and just bring me the source code of the login.php page (asking me to log in).

Thanks again for all of your help.
View user's profile Send private message Send e-mail AIM Address Widgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Powered by phpBB © 2001, 2002 phpBB Group