2017-03-01 4 views
0

웹 페이지에서 지정한 pastebin 파일의 원시 데이터를 가져와야합니다. 그냥 http://pastebin.com/qnNPx6G9이라고 말하고 변수로 저장하십시오. xml 및 ajax 요청에 많은, 많은, 많은 변형을 시도했지만 아무것도 작동하지 않습니다. 여기 내가 시도한 것이있다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?HTML/자바 스크립트 - 원시 pastebin에서 데이터 가져 오기

나는 Ajax를 시도했다 :

$.ajax({ 
url: "http://pastebin.com/api/api_post.php", 
type: "GET", 
dataType: "x-www-form-urlencoded", 
data: { 
    "api_dev_key": "mydevkey", 
    "api_option": "paste", 
    "api_paste_code": "blah blah" 
}, 
success: function(res) { 
    alert(JSON.stringify(res)); 
}, 
error: function(res) { 
    alert(JSON.stringify(res)); 
} 
}); 
//this is in the form of create paste, because I was seeing if it would work where get did not- it didn't. 

을 그리고 정기적으로 XMLHttpRequest를 가진 :

var xhr = new XMLHttpRequest(); 
xhr.open('POST'/*also tried GET*/, 'http://pastebin.com/raw/qnNPx6G9', true); //I've also tried /raw.php?i=qnNPx6G9 
xhr.onreadystatechange = function() { 
     if (xhr.readyState == 4) { 
     alert(this.responseText); 
     } 
}; 
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
xhr.send("api_option=trends&api_dev_key=DEVKEY"); 
//I tried trends because creating a paste and getting a paste didn't work. 

이 도와주세요! 이것이 어리석은 질문이거나 불분명 한 것이면 미안합니다. API를 잘 이해하지 못합니다. 감사!

아니요, 저는 PHP를 사용할 수 없습니다.

+0

아약스 CORS에 대해 자세히 알아보십시오. –

+0

질문과 관련이 없지만 JSON을 경고하는 대신 디버깅 할 때 [console.log'] (https://developer.mozilla.org/en/docs/Web/API/Console/log)를 반드시 확인해야합니다. 브라우저 devtools도 좀 더 유용한 정보를 표시 할 수 있습니다 :) – Scott

+1

가능한 [JQuery Ajax 요청의 중복 http://pastebin.com/raw.php] (http://stackoverflow.com/questions/16449492/jquery-ajax-request -to-http-pastebin-com-raw-php) – nicholas79171

답변

0

당신은 콘솔이 오류가 나타으로 허용하지 않는 분명히 페이스트 빈 CORS 요청을 만들려고 :

No 'Access-Control-Allow-Origin' header is present on the requested resource.

내가 유일한 옵션을 위해 서버 측 프로그래밍 언어를 사용하는 것입니다 생각을 원격으로 pastebin에 액세스하면 CORS 요청은 원격 서버가 권한을 부여한 경우에만 허용됩니다. 그렇지 않은 경우 CORS 요청을 무시할 방법이 없습니다. 서로 다른 도메인 사이트에서 아약스를 사용할 수 없습니다 domain.so

는 교차 할 수 없습니다 here