Mdbook
Mdbook

Reputation: 39

HTML/Javascript- get data from raw pastebin

I have a webpage in which I need to just get the raw data of a specified pastebin file, let's just say http://pastebin.com/qnNPx6G9, and store it as a variable. I've tried many, many, many variations on xml and ajax requests, but nothing works. Here's what I've tried. What am I doing wrong?

I've tried with 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.

And with regular 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.

Please help! I'm sorry if this is a stupid question or anything is unclear, I'm not that good at understanding APIs. Thanks!

And no, I can't use PHP.

Upvotes: 1

Views: 4497

Answers (1)

nomorehere
nomorehere

Reputation: 332

You are trying to make a CORS request that pastebin obviously doesn't allow as console shows up this error:

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

I think your only option is to use a server-side programming language in order to access pastebin remotely, CORS requests are only allowed just in the case the where remote server authorised it, else you have no way to bypass it.

Read more about CORS here

Upvotes: 1

Related Questions