简体   繁体   English

Ajax跨域PHP代理403错误

[英]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: 我正在尝试使用javascript和PHP代理消耗第三方Api,如本Tread所示 ,我可以使用代理,但得到的响应始终是:

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 无法加载资源:服务器的响应状态为403(禁止) http://MYDOMAIN.co/php/ba-simple-proxy.php?url=http://jsonplaceholder.typicode.com/posts&_=1471620448707

my javascript code is: 我的JavaScript代码是:

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? 我以为这与第三方服务器上的权限有关,所以我决定将其更改为一个开放的服务器-http://jsonplaceholder.typicode.com/posts-但我仍然遇到相同的错误,它可能是我的权限自己的服务器? -my host is hostgator- -我的主人是hostgator-

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 . api端不提供跨站点访问。 So we can use dataType: 'jsonp' to overcome this issue 所以我们可以使用dataType: 'jsonp'来解决这个问题

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. 出于安全原因,您无法向其他域发出ajax请求,因为恶意攻击还可能涉及通过ajax发出请求以加载其他脚本来入侵您。

Even though Wikipedia might not be the best link to provide, it'll give you an idea. 即使Wikipedia可能不是提供的最佳链接,它也会带给您一个想法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM