2012-07-27 2 views
2

sammyjs를 사용하여 외부 API에서 json을로드하는 방법이 확실하지 않습니다.SammyJS에서 json을로드하는 중

this.get('#/contact', function(context) { 
     this.load('somefile.json') 
     .then(function(items) { 
      $.each(items, function(i, item) { 
       context.log(item); 
      }); 
     }); 

    }); 

HTTP를 통해로드 같은 JSON이 실패하지 않습니다하지만 :

이 코드는 잘 작동

this.get('#/contact', function(context) { 
     this.load('http://samedomain/api/getdata') 
     .then(function(items) { 
      $.each(items, function(i, item) { 
       context.log(item); 
      }); 
     }); 

    }); 

HTTP를 통해로드, 새미가 더 이상 개체로 JSON을보고하고, 구문 분석에 나타납니다 텍스트 데이터.

모두가 분명하므로 도메인 액세스에 문제가 없습니다.

header('Access-Control-Allow-Origin: *'); 

아니요 로컬 파일로로드 할 때 정상적으로 작동하는 것으로 보아 내 json 형식의 문제라고 생각합니다.

내 나머지 API는 useing된다

"Content-Type: application/json; 

업데이트 : 워드 프레스에서 사용하고 다른 사람이 할 수있는 경우에 여기를 나열하려면이 옵션을 넣어

(function($) {  
var app = $.sammy('#main', function() { 
    this.use('Template'); 

    this.helpers({ 
      loadJSON: function(location, options, callback) { 
       options = $.extend(options, {json: true}); 
       return new Sammy.RenderContext(this).load(location, options, callback); 
      } 
     }); 


    this.get('#/', function(context) { 
     this.loadJSON('http://localhost/wp-somesite/wp-admin/admin-ajax.php?action=get_all_cases') 
      .then(function(items) { 
       $.each(items, function(i, item) { 
        context.log(item); 
       }); 
      }); 
     }); 

    }); 

$(function() { 
    app.run('#/'); 
}); 
}) (jQuery);

답변

2

문제는 sammy.js가 검색하는 데이터 유형을 결정하는 방법과 관련이 있습니다. "mydata.json"을로드하면 .json이 sammy.js에게 JSON 데이터임을 알립니다. 그러나 ''http : // samedomain/api/getdata ''를로드 할 때 일반 텍스트 데이터로 간주합니다. 과 같이

나는 가장 좋은 방법은 어떻게 될지 모르지만 두 가지 솔루션 (...) 라우팅을 변경하거나 this.json 사용하여 JSON에로드 된 항목 수를 변환하는 중입니다

this.get('#/contact', function(context) { 
    this.load('http://samedomain/api/getdata') 
    .then(function(items) { 
     $.each(this.json(items), function(i, item) { 
      context.log(item); 
     }); 
    }); 

}); 

앱에 this.use (Sammy.JSON)가있는 JSON 라이브러리가로드되고 JSON 플러그인이 스크립트 정의에로드되어 있는지 확인하십시오.

편집 : 당신은 JSON을로드 알고있는 사용자 정의 함수를 작성할 수있는 또 다른 옵션은, 여기 당신이 사용할 수있는 예제 플러그인은 다음과 같습니다

Sammy.JSON.LoadJSON.js :

(function($) { 
    Sammy.JSON.LoadJSON = function(app) { 
     app.helpers({ 
      loadJSON: function(location, options, callback) { 
       options = $.extend(options, {json: true}); 
       return new Sammy.RenderContext(this).load(location, options, callback); 
      } 
     }); 
    } 
})(jQuery); 

app.js :

이 VoDurden 말한 외에도
this.use(Sammy.JSON.LoadJSON); 

var app = $.sammy('#mytag', function() { 
    this.get('#/contact', function(context) { 
     this.loadJSON('http://samedomain/api/getdata') 
     .then(function(items) { 
      $.each(items, function(i, item) { 
       context.log(item); 
      }); 
     }); 
    }); 
}); 
+0

나는이 소용돌이 줄 것이다. 이것을 텍스트로 보았을 때 이제는 의미가 있습니다. 도와 줘서 고마워! – alloyking

5

, 당신은 또한 폭발물로드 옵션을 전달할 수 있습니다 데이터 유형을 명시 적으로 설정하십시오. Sammy Load function on Github을 보면 ajax 호출에 대한 래퍼라는 것을 알 수 있습니다. 이 같은

시도 뭔가 :

loadOptions = { 
       type: 'get', 
       dataType: 'json', 
       data: {query: variable}, 
      }; 

context.load("http://path.com/api/", loadOptions); 
+0

매우 도움이됩니다. 고맙습니다. – alloyking

+0

이것은 나를 위해 일했습니다! – Maarten

+1

이것이 올바른 대답이라고 생각합니다. – emostafa

관련 문제