1

I know there are so many explanations out there, but it still doesn't solve my current problem. Basically, I was using /sth and ``/sthElseto render into my index.html (which is/`) because one is post another one is comments, I did include the header in my server, but but I still got an error says:

XMLHttpRequest cannot load file:///sth. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource."

    //client jquery ajax

    function something(){
      $.ajax({
        method: 'GET',
        url: '/sth',
        headers: headers,
        success: function(data) {
          console.log(data);
        }
      });
    }
    function somethingElse(){
  $.ajax({
    method: 'GET',
    url: '/sthElse',
    success: function(data) {
      console.log('comment',data);
        }
      }
    }
  });
}


    // server express

    app.use(function(req, res, next) {
      res.header('Access-Control-Allow-Origin', 'example.com');
      res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
      res.header('Access-Control-Allow-Headers', 'Content-Type');
      next();
    });

    app.get('/sth', function(request, response){
      var req = http.get(url+"posts", function(res){
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        var bodyChunks = [];
        res.on('data', function(chunk) {
          bodyChunks.push(chunk);
        })
        .on('end', function() {
          var body = Buffer.concat(bodyChunks);
          response.send(JSON.parse(body));
        })
      });
      req.on('error', function(e) {
        console.log('ERROR');
      });
    });

app.get('/sthElse', function(request, response){
  console.log('STATUSasdf: ' + response.statusCode);
  var req = http.get(url+'comments', function(res) {
    var bodyChunks = [];
    res.on('data', function (chunk) {
      bodyChunks.push(chunk);
    })
    .on('end', function() {
      console.log('this is bodychunks pre decription', bodyChunks);
      var body = Buffer.concat(bodyChunks);
      response.send(JSON.parse(body));
    })
  });
  req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
  });
});
Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
Someone
  • 157
  • 2
  • 3
  • 10
  • 1
    You can't run ajax from a `file://` protocol, you'll always end up with CORS errors. You need a webserver, and it looks like you have one, so why are you running on `file://` ? – adeneo Oct 09 '15 at 06:51

1 Answers1

1

I did include the header in my server

There is no server involved here.

You are loading the HTML document from a local file. You are using a relative URL to another local file.

You should load both files over http, which should resolve the problem.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I changed my ajax url to http://localhost:3000/sth and got the error said "Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers." – Someone Oct 09 '15 at 07:39
  • 1
    So don't put `access-control-allow-origin` in the request headers (there isn't any sign in the question that you are, but you haven't shown us the value of the `headers` variable). `access-control-allow-origin` is a response header. It would be pretty stupid for your JavaScript to be able to grant itself permission to access another website. – Quentin Oct 09 '15 at 08:25
  • Hi. I already got it. Basically, I need to set cross-domain to true in my ajax – Someone Oct 09 '15 at 14:24
  • No. A cross domain request is cross domain if it crosses domains. That isn't something you set (other than by using a URL which is on a different domain). You are trying to access your own service on the same domain. Just use a relative URL and make sure you load your HTML from the server. – Quentin Oct 09 '15 at 14:26