0

I want to get data from webpage on another domain but i get this error

No 'access-control-allow-origin' header is present on the requested resource

I have tried CORS example from this blog http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/ but i still get the same error.
I have managed to get the content of the page when i ran it in application (same thing i did in Titanium app, now working in PhoneGap that runs in browser)

This is my script

function createCORSRequest(method, url){
 var xhr = new XMLHttpRequest();
 if ("withCredentials" in xhr){
     xhr.open(method, url, true);
 } else if (typeof XDomainRequest != "undefined"){
     xhr = new XDomainRequest();
     xhr.open(method, url);
 } else {
     xhr = null;
 }
 return xhr;

}

function insertText () {
var artist = [];
var table = document.createElement('TABLE')
var tableBody = document.createElement('TBODY')
table.border = '1'
table.appendChild(tableBody);
var xhr = createCORSRequest('GET', "http://www.rockwerchter.be/en/line-up");
if (!xhr) {
  throw new Error('CORS not supported');
}
xhr.open("GET", "http://www.rockwerchter.be/en/line-up");
xhr.send();
xhr.onload = function(){
    text = request.responseText;
    var regex = new RegExp(".*box-title box-title-h3 mb-05.*", "g");
    artist = text.match(regex);
    var data = [];
    for (var i=0; i<artist.length; i++) {
        var tr = document.createElement('TR');
        tableBody.appendChild(tr);
        artist[i]= artist[i].replace("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<h2 class=\"box-title box-title-h3 mb-05\">", "");
        artist[i]= artist[i].replace("</h2>","");
        tr.innerHTML=artist[i];
    }
}
}
sparrkli
  • 661
  • 1
  • 7
  • 17

1 Answers1

0

If the requested url is a PHP file, you should add this header on top of your page:

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

Instead of the wildcard, you could enter the domain you are requesting it from.


Based on your reply, you should set it in .htaccess as following:

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

I got this from this source

Community
  • 1
  • 1
Arko Elsenaar
  • 1,689
  • 3
  • 18
  • 33
  • requested url is html file – sparrkli May 14 '14 at 09:37
  • Sorry but i am lost, where should i put this? Thank you verya much for your time! – sparrkli May 14 '14 at 09:46
  • You should create a .htaccess file in the same folder as your html file is in you requested. Inside this .htaccess file you can add this code. Other solution is to just simply change the file to .php and add my first solution. – Arko Elsenaar May 14 '14 at 09:46
  • I mean, where is this .htaccess file – sparrkli May 14 '14 at 09:47
  • As I said, you should create it if its not there. – Arko Elsenaar May 14 '14 at 09:48
  • So i have added the .htaccess file, downloaded the plugin (intellij ide), and put the following code but still not working `Header add Access-Control-Allow-Origin "*" Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type" Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"` – sparrkli May 14 '14 at 09:59
  • Is the domain you request, your domain? the line-up website – Arko Elsenaar May 14 '14 at 10:01
  • i guess not, not my domain. that what is causing the problem, right? – sparrkli May 14 '14 at 10:04
  • If those are just HTML files, why would you allow `PUT, POST, DELETE` methods? – Bergi May 14 '14 at 12:12
  • @Bergi, this was just a quick copy from my code. You are right. And OP: that is a problem yes. You have to enable it on the side you are getting data from. What you could do, is create an ajax file on your website, that uses file_get_contents() to get the html of the source file. – Arko Elsenaar May 14 '14 at 12:14