2014-05-24 5 views
2

이 내 JSONP 파일입니다 사실, "오류" : "", "data": { "schedule": this site에서 보았습니다. json_encode가있는 곳입니다.JSONP와 콜백하는 데 도움이 필요한 오류 수정

그러나 테이블에 데이터를 표시 할 수는 없지만 http://www.myradio1.localhost/onairschedule.php에서 코드를 화면에 표시합니다.

이 실제 코드가 http://www.myradiostations.localhost/schedule.php

에 저장하고 콜백 함수는 하나의 예는 http://www.myradiostations.localhost/schedule.php?callback=?&name=Northern+FMhttp://www.myradiostations.localhost/schedule.php?callback=?&name=Southern+FM되고, 확인을 작동하지만 무엇 내가이 예에서와 같이 변경하기 위해 어떻게해야합니까 : this examplethis example 및 그러한 파일이없는 경우 this과 같은 오류 메시지를 생성합니다.

중간에 있는데, 코드 수정에 대한 조언이 필요합니다.

var doFades = true; 

var LocalhostRadioStations = { 
    Schedule : {} 
}; 

$(document).ready(function(){ 

    LocalhostRadioStations.Schedule.days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] 

    LocalhostRadioStations.Schedule.Show = function() { 
    _s = null; 
    _startDate = null; 
    _endDate = null; 
    this.days = LocalhostRadioStations.Schedule.days; 
    _selector = ''; 

    this.setShow = function(s) { 
     this._s = s; 

     this._startDate = new Date(parseInt(s.startminutes, 10) * 1000); 
     this._endDate = new Date(parseInt(s.endminutes, 10) * 1000); 

    }; 

    this.getEndDate = function(){ 
     return this._endDate; 
    } 

    this.getStartDate = function(){ 
     return this._startDate; 
    } 

    this._getShowDay = function(){ 
     return this.days[this.getStartDate().getDay()]; 
    }; 

    this._getShowUnitsTaken = function(){ 
     // if it's the same day 
     return this._getEndUnits() - this._getStartUnits(); 
    }; 

    this._getEndUnits = function(){ 
     if(this.getEndDate().getHours() == 0) 
     { 
     //console.log(this._s.longname +' ends at midnight'); 
     return 48; 
     } 

     return this.getEndDate().getMinutes() !== 0 ? (this.getEndDate().getHours() * 2) : (this.getEndDate().getHours() * 2); 
    }; 

    this._getStartUnits = function(){ 
     if(this.getStartDate().getHours() == 0) 
     { 
     return 0; 
     } 

     return this.getStartDate().getMinutes() !== 0 ? (this.getStartDate().getHours() * 2) : (this.getStartDate().getHours() * 2); 
    }; 

    this.getCellPositions = function() { 
     return { 
     'start' : this.getStartDate(), 
     'end' : this.getEndDate(), 
     'colIndex' : this.getStartDate().getDay() + 2, 
     'startUnits' : this._getStartUnits(), 
     'endUnits' : this._getEndUnits(), 
     'unitsTaken' : this._getShowUnitsTaken() 
     } 
    }; 

    this.pad = function(number){ 
     return number < 10 ? '0'+number : number; 
    }; 

    // return the table cell html. 
    this.toHtml = function() { 

     var d = new Date(); 
     var units = this._getStartUnits(); 

     var rowspan = this._getShowUnitsTaken();  
     var desc = this._s.description; 
     var name = this._s.longname; 

     var starttime = this.pad(this.getStartDate().getHours()) + ':' + this.pad(this.getStartDate().getMinutes()); 
     var endtime = this.pad(this.getEndDate().getHours()) + ':' + this.pad(this.getEndDate().getMinutes()); 

     var site = this._s.websitelink; 

     var cls = this.isActive() ? 'current-program' : ''; 

     var isToday = this.getStartDate().getDay() === d.getDay() ? 'active-program' : ''; 

     var html = '<td class="schedule-show ' + isToday + ' ' + cls + '" rowspan="' + rowspan + '" data-start="' + this.getStartDate() + '" data-end="' + this.getEndDate() + '">'; 
     html += '<div>'; 
     html += '<a href="' + site + '" title="' + desc + '">' + name + '</a>'; 
     html += '</div>'; 
     if(doFades) 
     { 
      html += '<div class="schedule_details clearfix" style="display:none;">'; 
      html += '<img width="105px" height="105px" alt="' + desc + '" src="' + this._s.showimage + '">'; 
      html += '<strong>' + name + '</strong>'; 
      html += '<p>' + desc + '</p>'; 
      html += '<span>' + starttime + ' - ' + endtime +'</span>'; 
      html += '</div>'; 
     } 
     html += '</td>';  

     return html; 
    }; 

    this.setTableSelector = function(sel){ 
     this._selector = sel; 
    }; 

    // check if we should add the active class. 
    this.isActive = function(){ 
     var t = new Date(); 

     return t >= this.getStartDate() && t <= this.getEndDate(); 
    }; 
    }; 

    LocalhostRadioStations.Schedule.ScheduleGen = function(){ 

    return { 
     insertShow : function(show) { 
     var p = show.getCellPositions(); 

     $('tr#units-' + p.startUnits).append(show.toHtml()); 
     }, 

     init : function (stationName){ 
     var self = this; 

     // load the schedule. 
     $.getJSON('http://www.myradiostations.localhost/schedule.php?callback=?&name=', { 
      name: 'Northern FM' 
     }, function(json){ 
      // loop each show and append to our giant table. 
      // this is well sick. 

      if(json.success === false) 
      { 
      $('.content-inner table').remove(); 

      $('<div>errors</div>').appendTo('.content-inner'); 
      } 
      else 
      { 
      var currentDay = ''; 
      var day = 0; 
      // highlight the current time.. 
      var d = new Date(); 
      var weekStart = new Date(); 

      weekStart.setDate(d.getDate()-6-(d.getDay()||7)); 

      $.each(json.data.schedule, function(i, broadcast){ 


       var dStart = new Date(parseInt(broadcast.startminutes, 10) * 1000); 
       var dEnd = new Date(parseInt(broadcast.endminutes, 10) * 1000); 


       /*// transform to a show object defined above, if the show spans 2 days we create two show objects. 
       // IF THE SHOW STARTS/ENDS AT MIDNIGHT, DON'T SPLIT IT. 
       if(dStart.getHours() !== 0 && dEnd.getHours() !== 0 && dStart.getDate() != dEnd.getDate()) 
       { 
       var showOne = new LocalhostRadioStations.Schedule.Show(); 
       showOne.setShow(broadcast); 

       // set to midnight 
       showOne.getEndDate().setHours(0); 
       showOne.getEndDate().setMinutes(dStart.getMinutes()); 


       // append first half of show. 
       self.insertShow(showOne); 

       // handle second half. 
       var showTwo = new LocalhostRadioStations.Schedule.Show(); 
       showTwo.setShow(broadcast); 

       showTwo.getStartDate().setDate(showTwo.getStartDate().getDate() + 1); 
       showTwo.getStartDate().setHours(0); 
       showTwo.getStartDate().setMinutes(dEnd.getMinutes()); 

       //console.log('2nd Half Start: ' + showTwo.getStartDate()); 
       //console.log('2nd Half End: ' + showTwo.getEndDate()); 

       self.insertShow(showTwo); 


       } 
       else 
       {*/ 
       var show = new LocalhostRadioStations.Schedule.Show(); 

       show.setShow(broadcast); 
       show.setTableSelector('table#schedule'); 

       // add the show to the table. Thankfully the order these come out the API means they get added 
       // in the right place. So don't change the schedule builder code! 

       self.insertShow(show); 
       //} 


      }); 

      var days = LocalhostRadioStations.Schedule.days; 

      // apply the current day/time classes 
      $('th:contains('+ days[d.getDay()]+')').addClass('active'); 

      $('td.time').each(function(i, cell){ 
       // get the value, convert to int. 
       var hours = $(cell).html().split(':')[0]; 

       // compare the hours with now, add class if matched. 
       if(parseInt(hours, 10) === d.getHours()) 
       { 
       $(cell).addClass('current_time'); 
       } 
      }); 
      } 


      if(doFades) 
      { 
      // apply events to show info fade in/out. 
      $('td.schedule-show').hover(function(){ 
       $(this).find('.schedule_details').fadeIn('fast'); 
      }, function(){ 
       $(this).find('.schedule_details').fadeOut('fast'); 
      }); 
      } 
     }); 


     } 
    }; 
    }(); 


    LocalhostRadioStations.Schedule.ScheduleGen.init(twittiName); 

}); 
:

기본적으로, 나는 할 노력하고있어 ... 내 페이지에 내 자바 스크립트 파일의 URL에 따라 컨텐츠를 다양 내 HTML 테이블에서 작동 할 수있는 JSONP를 얻을

JSONP에 따라 일정이 변경되어야하지만 수정하려면 어떻게해야합니까?

기본적으로 http://radioplayer.bauerradio.com/schedule.php?callback=&name=Rock+FM 및 해당 JSON/JSONP의 로컬 호스트 버전을 만들려고합니다. (원래 어떤 유형인지는 확실하지 않지만 사이트는 실험용이며 .localhost 도메인에 있습니다.) 테스트 용입니다. 콘텐츠는 데이터베이스에서 가져온 목적이며 역 이름에 따라 변경됩니다 (예 : http://radioplayer.bauerradio.com/schedule.php?callback=&name=Metro+Radiohttp://radioplayer.bauerradio.com/schedule.php?callback=&name=Forth+One

편집 : 내 페이지의 전체 코드는 내가 실종 정확히 모르겠지만, 지금까지 준 코드에서, 나는 jsfiddle을 만들어 http://pastebin.com/ENhR6Q9j

답변

3

에서 볼 수있다 :

Demo

은 내가 원래 HTML 파일을 모르기 때문에 그것이 (주로 물건을 추가) 작동되도록하려면 몇 가지를 수정했습니다. 그러나 나는 또한 네가 원하는 말을 토대로 몇 가지 변화를 만들었다. 우선 내가 좋아하는 뭔가 당신의 $.getJSON 전화를 수정 :

LocalhostRadioStations.Schedule.ScheduleGen.init(twittiName); 

에 전달되는 내용에 따라 역을 다시 제공해야

$.getJSON('http://radioplayer.bauerradio.com/schedule.php?callback=?', { 
      name: stationName //stationName is from the argument passed 
     }, function(json){...}) 

이 더 흥미로운 만들기 위해, 나는 또한 약간 추가 URL에서 읽는 코드 이 경우 domain.htm/page?Northern FM이있는 페이지로 이동하면 ? 뒤에 나오는 텍스트를 읽고 twittiName에 넣습니다.

var twittiName="Rock FM"; //default station 

if(window.location.search){ 
    twittiName=window.location.search.substring(1) //or window.location.hash 
} 

귀하의 퍼블릭 사이트에있는 다른 방송국을 찾으려고했지만, 지금까지 "? Rock + FM"으로 만 테스트 할 수있었습니다. 그러나 코드에서 처리 할 수있는 오류를 표시 할 수 있음을 의미합니다.

  • ?Random Name
  • ?Rock+FM
    • 그래서 당신의 코드는 대부분 내가 놓친 경우 만 의견을 작동하는 것 같다.

    +0

    해답을 가져 주셔서 감사합니다. 정말 유용합니다. 내가하려는 것은 온라인에 연결된 JSON (공개 사이트) 인 bauerradio 도메인을 사용하는 것이 아니라 내 데이터베이스의 코드를 사용하는 것이다. 내 코드는 http://pastebin.com/ENhR6Q9j입니다. 기본적으로 로컬 호스트 테스트를 위해 내 버전을 만들려고합니다. – avenas8808

    관련 문제