English 中文(简体)
jQuery "getJSON" from an external domain that doesn t support JSON-P output
原标题:

I m trying to grab data from a JSON on an external site but the site doesn t support JSON-P output. This is an example of non-working code, but gives a good idea of what I m trying to achieve:

$.getJSON("http://www.bom.gov.au/fwo/IDV60901/IDV60901.94868.json", function(data){
    //Process data here
});

Are there ways around this other than locally hosting the data or downloading and processing it with an AJAX/PHP call? I would rather not have the server serve or download the data and rather have the user s browser grab it directly.

Thanks in advance!

最佳回答

The Same Origin Policy of most browsers would not let you do this without a willing external server or a server-side proxy. There are a few hacks you could try out with flash:

http://flxhr.flensed.com/

This assumes your user has flash installed, but generally, if they have javascript installed, they also have flash...

OR

If the data you are looking for came as a feed somewhere, you could pass it through Yahoo Pipes and they will return jsonp for you.

Best of luck!

问题回答

Easiest option would be to run the json call through a PHP proxy script, like this one:

<?php
// PHP Proxy
// Loads a file from any location.
// Author:Paulo Fierro
// January 29, 2006
// usage: proxy.php?url=http://mysite.com/myxml.xml

$session = curl_init($_GET[ url ]);                    
curl_setopt($session, CURLOPT_HEADER, false);          
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);   
$xml = curl_exec($session);                            
echo $xml;        
curl_close($session); 

?>

and use that as the source of you ajaxCall

$.getJSON("proxy.php?url=http%3A%2F%2Fwww.bom.gov.au%2Ffwo%2FIDV60901%2FIDV60901.94868.json", function(data){




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签