2011-12-23 4 views
2

API (instapaper)에 ajax 요청을 보내고 성공적인 요청을 할 수있었습니다. 즉, instapaper에 대한 링크를 성공적으로 게시하고 받았습니다. 정확한 상태 코드 - 201. 그러나 성공 콜백 함수 대신 오류 콜백 함수가 호출됩니다. 이것이 내 요청을 설정 한 방식 때문이라고 생각합니다. 예 : 그것은 다른 형식으로 응답을 기대하고 있습니다.

요청 기능 :

$("#instapaper").click(function() { 
    $.ajax({ 
     type: 'GET', 
     dataType: 'jsonp', 
     url:'http://www.instapaper.com/api/add', 
     data: {"url": ref , "username": "<%= current_user.instapaper_user %>", "password": "<%= current_user.instapaper_pass %>" }, 
     context: document.body, 
     error: function() { 
      alert('There was an error!'); 
     }, 
     success: function() { 
      alert('Page sent'); 
     }, 
     }) 
    }); 

내가 코드 (201)에 대한 상태 코드 콜백을 사용하여 시도했지만, 그 중 하나가 작동하지 않습니다. 도움을 주시면 감사하겠습니다.

+0

'error : function (data) { alert (data.responseText);'를 사용하여 유효한 JSON 인 경우 응답을 잡아 봅니다. }' – Usman

+0

[jQuery doco] (http://api.jquery.com/jQuery.ajax/) _에 따르면 정말 이상합니다. "원격 서버에서 데이터를 검색하는 경우 (스크립트를 사용하거나 jsonp 데이터 유형) 오류 콜백 및 전역 이벤트는 절대로 실행되지 않습니다. "_ – nnnnnn

답변

1

것 같습니다. instapaper doco에 따르면

$.ajax({ 
    type: 'GET', 
    dataType: 'jsonp', 
    url:'http://www.instapaper.com/api/add', 
    data: {"url": ref , "username": "<%= current_user.instapaper_user %>", "password": "<%= current_user.instapaper_pass %>" }, 
    context: document.body, 
    error: function() { 
     alert('There was an error!'); 
    }, 
    success: function() { 
     alert('Page sent'); 
    }, 
    jsonp: 'jsonp' 
}); 
0

this discussion, 나는 상태 코드를 직접 처리하려고 시도 할 때 성공 함수를 제거해야한다고 생각합니다.

는이 같은 statuscodes 옵션을 사용하려고 다음 instapaper API가 콜백 함수 이름으로 jsonp 필요하지만, jQuery를 기본적는 callback처럼

statusCode: { 
    200:function() { alert("200"); }, 
    201:function() { alert("201"); } 
} 
0

의 JSONP 콜백을 지정하는 매개 변수는 "JSONP"반면, jQuery를 기본값을 호출하도록되어 :

나는 그것이 작동하는 경우에 당신이 당신의 아약스 객체에 jsonp: 'jsonp'를 추가하고 참조 제안 "콜백". 당신은 "JSONP"옵션을 지정하여이를 대체 할 수 있습니다 :

$("#instapaper").click(function() { 
    $.ajax({ 
     type: 'GET', 
     dataType: 'jsonp', 
     jsonp : 'jsonp', // <---------------------NEW BIT HERE 
     url:'http://www.instapaper.com/api/add', 
     data: {"url": ref , "username": "<%= current_user.instapaper_user %>", "password": "<%= current_user.instapaper_pass %>" }, 
     context: document.body, 
     error: function() { 
      alert('There was an error!'); 
     }, 
     success: function() { 
      alert('Page sent'); 
     }, 
     }) 
    }); 

이 더 많은 정보를 위해 jQuery doco를 참조하십시오.