Carlos Valencia
Carlos Valencia

Reputation: 6963

Ajax Cross-domain Php proxy 403 error

I'm trying to consume a third party Api using javascript and a PHP proxy as seen in this Tread, i'm able to use the proxy but the response I get, is always:

Failed to load resource: the server responded with a status of 403 (Forbidden) http://MYDOMAIN.co/php/ba-simple-proxy.php?url=http://jsonplaceholder.typicode.com/posts&_=1471620448707

my javascript code is:

function getLocationSimple(){
var proxy = 'php/ba-simple-proxy.php',
    url = proxy + '?url=' + 'http://jsonplaceholder.typicode.com/posts';
    console.log(url);
// Make JSON request.
  $.getJSON( url, function(data){

    console.log(data);
  });
}

I thought it was about permissions on the third party server, so i decided to change it to an open one - http://jsonplaceholder.typicode.com/posts -, but i still get the same error, it might be permissions in my own server? -my host is hostgator-

Upvotes: 0

Views: 606

Answers (2)

Achyut Krishna Deka
Achyut Krishna Deka

Reputation: 759

Let's try once this piece of code

function getLocationSimple(){
            $.ajax({
                type: 'POST',
                dataType: 'jsonp',
                url: "http://jsonplaceholder.typicode.com/posts"
            })  .done(function( data ) {
                console.log( data);
              });
        }

this happens due to Cross-Domain Policy. Cross site access is not available in the api side . So we can use dataType: 'jsonp' to overcome this issue

Upvotes: 1

user2521387
user2521387

Reputation:

This has something to do with the Cross-Domain Policy. You can't do ajax requests to another domain due to security reasons, because a malicous attack could also involve to do a request via ajax to load additional script to hack you.

Even though Wikipedia might not be the best link to provide, it'll give you an idea.

https://en.wikipedia.org/wiki/Same-origin_policy

Upvotes: 0

Related Questions