2012-01-03 2 views
0

웹 앱을 개발 중입니다. 여기서는 JavaScript로 팝업 창을 만들었습니다. 이제이 창 안쪽에 일기 예보를 만드는 것을 좋아합니다.일기 예보를 만드는 가장 쉬운 방법은 무엇입니까?

가장 쉬운 방법은 무엇입니까? Get weather from Yahoo with jQuery

$(".popup-button").click(function(evt){ 
//prevent default link behavior 
    evt.preventDefault(); 

//get id from clicked element 
var id = $(this).attr("id"); 

switch (id) { 
    case "popup-button-wetter": 
     //centering with css 
     centerPopup($("#popup-wrapper-wetter")); 
     //load popup 
     loadPopupadditional($("#popup-wrapper-wetter")); 
     //get weather 
     getWeather() 
     break; 
      ...... 


function getWeather() { 
    $.get("http://weather.yahooapis.com/forecastrss?w=782458&u=c", function(data){ 
        console.log("Data Loaded: " + data); 
       }); 
      } 
    }); 

을하지만이 오류가있어 :

여기처럼 한

은 XMLHttpRequest http://weather.yahooapis.com/forecastrss?w=782458&u=c를로드 할 수 있습니다. 원본 파일 : //은 Access-Control-Allow-Origin이 허용하지 않습니다.

어떤 의미입니까?

답변

1

자바 스크립트로 교차 도메인 아약스 요청을 할 수 없습니다.

1

나는 늦었지만 날씨 페이지를 만들 때 같은 문제에 부딪쳤다. 나는 구글의 API를 사용하지만, 당신은 오히려 쉽게 야후의 API에 대해이 작업을 다시 할 수 있어야한다 :

를 수행 같은 것을 PHP 파일에서 :

<?php 
$lat = explode(".", $_GET["lat"] * 1000000); //latitude returned by JS geolocation 
$lon = explode(".", $_GET["lon"] * 1000000); //longitude returned by JS geolocation 
$api = simplexml_load_string(utf8_encode(file_get_contents("http://www.google.com/ig/api?weather=,,," . $lat[0] . "," . $lon[0] . "&zoom=10&hl=de"))); 
echo $api->weather->current_conditions->temp_c->attributes()->data; //prints the current temperature in °C 
?> 

그런 다음 UTF로 HTML 페이지의 문자 집합을 설정 -11 위해 <head> 태그에

<meta charset="utf-8"> 

삽입하여 올바르게, O 및 ü 움라우트 등을 표시한다.

Tl; dr : PHP를 통해 XML 파일을 잡고 로컬 PHP 파일로 XMLHttpRequest를 보내면 교차 도메인 AJAX 블록을 우회 할 수 있습니다. ;)

관련 문제