6 Replies - 1558 Views - Last Post: 02 April 2011 - 02:06 PM

#1 nathanpc  Icon User is offline

  • SCIENCE!
  • member icon

Reputation: 112
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Ajax Returning Undefined

Posted 01 April 2011 - 02:11 PM

I've been trying to get the today currency from Yahoo Finance with a function, but I don't know why(maybe because I'm not familiar with Ajax), but everything that I'm getting is undefined from this code:
function get_currency(from, to) {
	var XMLHttp;  // Create the Ajax handler
	XMLHttp = new XMLHttpRequest();
	var url = "http://download.finance.yahoo.com/d/quotes.txt?s=" + from + to + "=X" + "&f=l1&e=.txt";
	
	XMLHttp.open("GET", url, true);
	XMLHttp.onreadystatechange = function() {
		if(XMLHttp.readyState == 4) {
			/* Once the server has completed its tasks display the result */
			var response = XMLHttp.responseText;
			//var parsed_reply = response.split(',');

			document.write(response);
        }
	}
	XMLHttp.send(null);
}

But I can't see anything wrong with it. Could someone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: Ajax Returning Undefined

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • Joined: 07-September 06

Re: Ajax Returning Undefined

Posted 01 April 2011 - 02:42 PM

It is quite possible that the code is fine. Ajax calls aren't allowed to go between multiple websites. So, if you are at a.com you can't send an ajax request to b.com (because they are different domains). It is a security measure they chose to implement in Javascript and can't be changed.

Hope that makes sense.
Was This Post Helpful? 1
  • +
  • -

#3 nathanpc  Icon User is offline

  • SCIENCE!
  • member icon

Reputation: 112
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Ajax Returning Undefined

Posted 01 April 2011 - 02:52 PM

For sure, it makes a lot of sense for me. By the way, is there any way I can do this? Because my code will be running locally, so I can't have server-side languages... :/
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • Joined: 07-September 06

Re: Ajax Returning Undefined

Posted 01 April 2011 - 03:53 PM

Why couldn't you have server-side languages? You just have to install them before they will run on your box (and there are a lot of tutorials out there showing how to do it for both windows and linux based machines).

If you can't use server-side langauges you could make a page scraper in python (or Java, or something else) which would fetch the page and save it to a file for you.

Otherwise you could try using their Javascript API (assuming they have one for the finance section of their site - they may not).

The easiest way of doing things would probably be either using their API or through a server-side langauge...
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Ajax Returning Undefined

Posted 01 April 2011 - 05:49 PM

even when your AJAX code itself is right, you will fail at line 13. document.write() when used after the page finished loading rewrites your page from scratch (deleting everything else before that).

didnít it strike odd that no AJAX tutorial uses document.write()?
Was This Post Helpful? 0
  • +
  • -

#6 nathanpc  Icon User is offline

  • SCIENCE!
  • member icon

Reputation: 112
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Ajax Returning Undefined

Posted 01 April 2011 - 05:55 PM

I can't because I'm using a framework called PhoneGap so it might be compiled into an application... :)

HTML, CSS, Javascript -> Compiled Application

This post has been edited by nathanpc: 01 April 2011 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 nathanpc  Icon User is offline

  • SCIENCE!
  • member icon

Reputation: 112
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Ajax Returning Undefined

Posted 02 April 2011 - 02:06 PM

Got it! This code is working perfectly, even if I try it on my server :)
function fetchXML() {
	var req = new XMLHttpRequest();
	var url = "http://download.finance.yahoo.com/d/quotes.txt?s=EURUSD=X&f=l1&e=.txt";
	
	req.open("GET", url, false)
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send(null);
	
	if(req.readyState == 4 && req.status == 200) {
		fullxml = req.responseText;
        document.getElementById('txtAmount').value = fullxml;
		//=alert(fullxml);
	} else {
		alert("state: " + req.state + "\nstatus: " + req.status);
	}
}


Thanks very much guys

This post has been edited by nathanpc: 02 April 2011 - 02:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1