Solved

Load Jason $.ajax

Posted on 2014-11-26
2
118 Views
Last Modified: 2014-11-27
I have two different URL's to JSON data - one works and the other does not.
My call to the openweathermap works fine, but the  citybikeNYC fails.
It looked to me like the structure was the same so not sure why this is happening.


//myURL = 'http://www.citibikenyc.com/stations/json';


myURL = 'http://api.openweathermap.org/data/2.5/find?lat=42.033449&lon=-93.630885&cnt=15&units=imperial';

//load data from external source
	$.ajax({
		url: myURL,
		dataType: 'json',
    	jsonp: false
	}).done(function(data) {
		console.log(data);
	console.log(data.count + ' data count');
		

	});

Open in new window

0
Comment
Question by:lvmllc
2 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40467444
Thats a cross origin policy error, they need to allow you to access the api or use jsonp
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40467498
you need your server to load this data, so instead using their address in your code, use your.
an easy server code with php :

proxy.php :
<?PHP
   header('Content-type: application/json');
   echo file_get_contents("http://www.citibikenyc.com/stations/json");
?>

Open in new window


your code now look like :
//myURL = 'http://www.citibikenyc.com/stations/json';
//myURL = 'http://api.openweathermap.org/data/2.5/find?lat=42.033449&lon=-93.630885&cnt=15&units=imperial';

myURL = 'proxy.php'; // if needed, change this the right path : where you put the proxy.php in your website folder

//load data from external source
	$.ajax({
		url: myURL,
		dataType: 'json',
    	jsonp: false
	}).done(function(data) {
		console.log(data);
	console.log(data.count + ' data count');
		

	});

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show confirmation alert in Jquery 14 60
JS Plugin Chaining 2 40
Refresh div via ajax without lost ccs <li> styles 8 31
javascript: add id amounts 5 46
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question