0

I have a little page and I need to get JSON from another domain. If make this:

  $.get( "http://dev.frevend.com/json/users.json", function( data ) {
    console.log(data);
    alert( "Load was performed." );
  });

I get an error. I understand why it throws this error, but I don`t know how to aviod it. I have not acces to the server.

XMLHttpRequest cannot load http://dev.frevend.com/json/users.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

I also tried to use JSONP, but as I understand in that case server should wrap response with callback function, because I got a SyntaxError.

Is it possible to make this request with JSONP? I tried

  $.ajax({
    url: "http://dev.frevend.com/json/users.json",
    dataType: "jsonp",
    jsonpCallback: "logResults"
  });

  function logResults(data) {
    console.log(data);
  }

But got

Uncaught SyntaxError: Unexpected token :

JSON is valid, I checked.

Arthur Kishinets
  • 147
  • 4
  • 15
  • 2
    possible duplicate of http://stackoverflow.com/questions/28547288/no-access-control-allow-origin-header-is-present-on-the-requested-resource-err . There are number of other question about it. – Nalin Aggarwal Aug 05 '16 at 13:52
  • The Server does not allow requests coming from other domains apart its own. This is a Server Side configuration and you can not change it without access to the Server. Check out : https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS – Strahdvonzar Aug 05 '16 at 13:52
  • whether http://dev.frevend.com/json/users.json is under your control ? @arthur – Nandakumar Aug 05 '16 at 13:57
  • No. And that is strange, because I'ts my task for a job and I thought that I`m missing something. – Arthur Kishinets Aug 05 '16 at 13:59

3 Answers3

0

You need to allow access in your project configuration. Below site has more information

http://enable-cors.org/server.html

Thanks,

Nagashree Hs
  • 843
  • 6
  • 18
0

Use JSONP in jquery for this purpose JSONP reference

-2

Try to add a header into your PHP file which is responsible for executing every request.

header('Access-Control-Allow-Origin', '*');
Sanjay S
  • 112
  • 13
  • 2
    Quoting OP: `I have not acces to the server` – A. Wolff Aug 05 '16 at 13:55
  • Mr. @A. Wolff: How could you mark it not useful as OP clearly mentioned that **No 'Access-Control-Allow-Origin' header is present on the requested resource.** – Sanjay S Aug 05 '16 at 17:51