2011-11-25 2 views
3

나는 텀블러의 어두운 세계의 깊이 탐구하기 위해 노력하고있어, 다음과 같은 오류 극복하는 방법을 이해할 수 없다 : 나는 그것을 할 수있다 생각JQuery와 아약스 - 텀블러의 API v2를

Uncaught SyntaxError: Unexpected token : 

을 왜냐하면 나는 json을 되찾고 있지만 jsonp를 사용하려고하기 때문입니다. 여기에 내가 보내려고 내용은 다음과 같습니다

$(function(){ 
$.ajax({ 
    type: "GET", 
    url : "http://api.tumblr.com/v2/blog/MyTumblrName.tumblr.com/info", 
    dataType: "jsonp", 
    data: { 
     api_key : "MyTumblrApi" 
    }, 
    success: function(data){ 
     console.log(data); 
    }, 
}); 
}); 

내가 얻을 200 OK 응답을, 데이터하지만 여전히 (이해가 안 등에 대해 알고 싶습니다)

위의 오류 Tumblr도 친절하게도 다음을 지적하지만 구체적인 내용은 분명하지 않습니다.

All requests made with HTTP GET are JSONP-enabled. To use JSONP, append jsonp= and the name of your callback function to the request. JSONP requests will always return an HTTP status code of 200 but will reflect the real status code in the meta field of the JSON response.

도움이 될 것입니다.

+0

왜 jsonp에 관심이 있습니까? json 응답을 사용하십시오 ... –

+1

JSON을 사용하는 경우 다음을 얻습니다. XMLHttpRequest는 http://api.tumblr.com/v2/blog/MYTUMBLRNAME.tumblr.com/info?api_key=APIKEY를로드 할 수 없습니다. Origin http : //.0.0.0.0:3000은 Access-Control-Allow-Origin에서 허용되지 않습니다. – Galaxy

+0

아 맞아, 그 의미가있다 –

답변

7

텀블러는 당신을 말하고 무엇을합니까 - 요청에 콜백 함수 이름을 추가

myJsonpCallback = function(data) 
{ 
    console.log(data); 
} 

$.ajax({ 
    type: "GET", 
    url : "http://api.tumblr.com/v2/blog/MyTumblrName.tumblr.com/info", 
    dataType: "jsonp", 
    data: { 
     api_key : "MyTumblrApi", 
     jsonp : "myJsonpCallback" 
    } 
}); 

======================= ===================

EDIT : console.log는 구문 오류입니다. 실제로이 코드를 테스트하지 않았습니다.

성공은 어떻게됩니까? 나는 정말로 모른다. 시도해보십시오. :) 아마도 호출 될 것이지만 데이터 매개 변수는 null 일 가능성이 높습니다. 여기서 문제는 jQuery가 콜백 매개 변수의 이름을 callback이라고 지정합니다. 여기서 Tumblr은 jsonp을 기다리고 있습니다. 200 응답 jQuery 가능성이 단순히 eval() s 응답, 이유는 myJsonpCallback 실제로 호출됩니다. 경우 당신이 jQuery를 사용하지 않으

+0

그게 효과가! 한 가지 질문은 '성공'기능과 어떻게 지금 작동합니까? – Galaxy

+0

@Galaxy 님이 답변을 업데이트했습니다. –

+0

나는 이것이 낡은 대답이라는 것을 알고 있지만 검색의 맨 위에 나왔다. 몇 가지 정보를 추가하고 싶습니다 :'dataType : "jsonp"'; 그러나 Tumblr API는 이제 "콜백"매개 변수를 수락하므로 더 이상이 방법을 사용하지 않아도됩니다.API 호출 (이 경우는'api_key')에 필요한 모든 것을 제공 한 다음 성공 콜백이 나머지를 처리하도록합니다. 원할 경우 일반적으로 외부 연습으로 "myJsonpCallback"을 정의 할 수 있지만 성공하면 호출 할 수 있습니다. –

3

:

var tumblrFeed = document.createElement('script'); 
tumblrFeed.setAttribute("src", "http://api.tumblr.com/v2/blog/{blog.tumblr.com}/posts?api_key={your api key}&jsonp=callback"); 
document.getElementsByTagName("head")[0].appendChild(tumblrFeed) 

function callback(data){ 
    console.log(data); 
} 

나는이 목적을 위해 간단한 함수를 만들었습니다 :

function jsonpRequest(opt){ 
    var params = ""; 
    var blogName = "{your blog name}"; 
    var api_key = "{api key}"; 
    if("selector" in opt){params = "id=" + opt.selector;} 
    if(("offset" in opt) && ("limit" in opt)){params = "limit=" + opt.limit + "&offset=" + opt.offset;} 
    if("callback" in opt){params += "&jsonp=" + opt.callback;}else{params += "&jsonp=callback";} 
    params += "&api_key=" + api_key;  


    var tumblrFeed = document.createElement('script'); 
    tumblrFeed.setAttribute("src", "http://api.tumblr.com/v2/blog/" + blogName + "/posts?" + params); 
    document.getElementsByTagName("head")[0].appendChild(tumblrFeed) 
} 

사용 방법 : jsonpRequest ({offset : 50, limit : 5});

function callback(data){do stuff here ...} 

대체 용도 : jsonpRequest ({오프셋 : 50 한계 : 5, 콜백 "nameOfMyAmazingCallbackFunction를"});

function nameOfMyAmazingCallbackFunction(data){do stuff here ...}