2016-06-30 2 views
3

모바일 장치에서 VueJS를 실행하는 데 문제가 있습니다.VueJS가 모바일에서 작동하지 않습니다.

http://codepen.io/techcater/pen/xOZmgv

HTML 코드 : 나는 여기에 프로젝트에 대한 링크입니다 copepen.io

에 기상 예측 응용 프로그램을 만들어

<div class="container-fluid text-center"> 
     <h1>Your Local Weather</h1> 
     <p> 
     {{location}} 
     </p> 
     <p> 
     {{temperature}} 
     <a @click="changeDegree">{{degree}}</a> 
     </p> 
     <p> 
     {{weather | capitalize}} 
     </p> 

     <img :src="iconURL" alt="" /> 
     <br> 
     <a href="https://ca.linkedin.com/in/dalenguyenblogger" target="_blank">by Dale Nguyen</a> 
<!-- <pre>{{$data | json}}</pre> --> 
    </div> 

JS 코드 :

new Vue({ 
     el: '.container-fluid', 

     data: { 
      location: "", 
      temperature: "", 
      degree: "C", 
      weather: "", 
      iconURL: "" 
     }, 

     created: function(){ 
      this.getWeather(); 
     }, 

     methods: { 
      getWeather: function(){ 
      var that = this; 

      this.$http.get("http://ipinfo.io").then((response) => { 
        console.log(response.data); 
        that.location = response.data.city + ", " + response.data.country; 

        // Get weather informaiton 
        var api = 'ebd4d312f85a230d5dc1db91e20c2ace'; 
        var city = response.data.city; 
        var url = "http://api.openweathermap.org/data/2.5/weather?q={CITY}&APPID={APIKEY}&units=metric"; 
        url = url.replace("{CITY}",city); 
        url = url.replace("{APIKEY}", api); 

        that.$http.post(url,{dataType: 'jsonp'},{ 
       headers : { 
       'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' 
      }}).then((response) => { 
        console.log(response.data); 
        that.temperature = response.data.main.temp; 
        that.weather = response.data.weather[0]['description']; 
        that.iconURL = "http://openweathermap.org/img/w/" + response.data.weather[0]['icon'] + ".png"; 
        }, (response) => { 
         // error callback 
        }); 

       }, (response) => { 
        console.log(response.data);    
       });    
      }, 

      changeDegree: function() { 
      if(this.degree == "C"){ 
       this.degree = "F"; 
       this.temperature = Math.round((this.temperature*9/5 + 32)*100)/100; 
      }else { 
       this.degree = "C"; 
       this.temperature = Math.round(((this.temperature - 32)*5 /9)* 100)/100; 
      } 
      } 
     } 
     }) 

노트북에서는 잘 작동하지만 모바일에서는 정상적으로 작동하지 않습니다. 처음에는 Codepen 덕분이라고 생각했습니다. 사이트를 돌 때 뭔가가 발생할 수 있습니다. 그러나, 내 웹 사이트에서 프로젝트를 만들 때도 작동하지 않습니다.

문제를 찾을 수 있습니까? 감사합니다,

+0

정확하게 이동 장치에 실패 뭐죠 :

여기서 새로운 코드는? –

+0

휴대 기기는 무엇입니까? Safari iOS, Android? – gurghet

+0

@YerkoPalma 아이폰을 사용하고 있으며 사파리와 크롬 브라우저를 사용해 보았습니다. –

답변

0

나는 이것을위한 해결책을 발견했다. 나는 지금 내 휴대폰에서 일한다. 나는 다른 브라우저에서도 작업 할 것이라고 믿는다. 문제는 일부 브라우저에서 ">"작업을 인식하지 못하기 때문에 변경했습니다.

getWeather: function(){ 
      var that = this; 

      this.$http.get('http://ipinfo.io', {'headers': { 
     'Origin': 'http://yourdomain.com'} 
      }).then(function(response) { 
        console.log(response.data); 
        that.location = response.data.city + ", " + response.data.country; 

        // Get weather informaiton 
        var api = 'ebd4d312f85a230d5dc1db91e20c2ace'; 
        var city = response.data.city; 
        var url = "https://crossorigin.me/http://api.openweathermap.org/data/2.5/weather?q={CITY}&APPID={APIKEY}&units=metric"; 
        url = url.replace("{CITY}",city); 
        url = url.replace("{APIKEY}", api); 

        that.$http.post(url,{dataType: 'jsonp'},{ 
       headers : { 
       'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' 
      }}).then(function(response) { 
        console.log(response.data); 
        that.temperature = response.data.main.temp; 
        that.weather = response.data.weather[0]['description']; 
        that.iconURL = "http://openweathermap.org/img/w/" + response.data.weather[0]['icon'] + ".png"; 
        }).then(function(){ 
         // error callback 
        }); 

       }).then(function(){ 
        console.log(response.data);    
       });    
      }, 
2

코드가 잘 작동하는 것 같습니다. 단, codepen에서 오류 XMLHttpRequest cannot load http://ipinfo.io/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://s.codepen.io' is therefore not allowed access.이 발생합니다.

당신은 크로스 기원을 가능하게 headers 옵션에 도메인 이름을 넣을 수 있습니다, 여기에 예입니다

this.$http.get('http://ipinfo.io', { 
    'headers': { 
     'Origin': 'http://yourdomain.com' 
    } 
}) 

참조 예 : 나는 또한 당신이 잘못된 순서로 vue.min.jsvue-resource.js 스크립트를 넣어 발견 http://bozue.com/weather.html

그 중 일부 오류가 발생할 수 있습니다, vue.min.js 첫 번째 장소에 있어야합니다.

+0

crossorigin.me를 사용하면 잘못된 위치가 표시됩니다. 모바일 장치에서도 작동합니까? 감사합니다. –

+0

답변이 업데이트되었습니다. 아직 확인하지는 않았지만 모바일 장치에서 http://bozue.com/weather.html을 확인할 수 있습니다. – Rifki

+0

헤더를 보내 주셔서 감사합니다. 그것은 위대한 작품! 모바일 사이트 정보. 여전히 작동하지 않습니다 : ( –

관련 문제