简体   繁体   English

简单代理无法获取JSON响应

[英]Simple Proxy Can't Get JSON response

I am trying to connect to a site via a php proxy (this one: https://raw.github.com/cowboy/php-simple-proxy/master/ba-simple-proxy.php ), and this is the JavaScript function that does something with the response: 我正在尝试通过php代理连接到站点(此站点: https : //raw.github.com/cowboy/php-simple-proxy/master/ba-simple-proxy.php ),这是JavaScript对响应有作用的函数:

Edit: The code now looks like this. 编辑:代码现在看起来像这样。 Still no cigar. 还是没有雪茄。

function exista(word) {

    alert(word);
    var proxy = 'ba-simple-proxy.php?url=';
    var uri = "http://api.wordreference.com/0.8/ff175/json/roen/" + word;
    uri = encodeURIComponent(uri);
    alert(proxy + uri);
    var cuvantulexista = 1;
    jQuery.ajax({
        "async": false,
        "url": proxy + uri,
        "dataType": 'json',
        "method": "GET",
        "success": function (data) {
            alert("HERE!");
            if (data.Error || data.Response)
                cuvantulexista = 0;
        }
    });
    return cuvantulexista;
}

The php file is situated in the same folder as the js file that contains the above function and in the public folder which contains the html file. php文件与包含上述功能的js文件位于同一文件夹中,并且位于包含html文件的公共文件夹中。

The problem is I don't get any response whatsoever and I can't really tell why. 问题是我什么也没有得到回应,我也无法真正说出原因。

I also included the php in the html like so: 我也将php包含在html中,如下所示:

<?php
    include("js/ba-simple-proxy.php");
?>

I am sorry for my extreme noobiness but it's my first time working with php and cross domain communication. 非常抱歉,这是我第一次使用php和跨域通信。 Thank you. 谢谢。 :) :)

Edit: The page is not hosted on a domain if that helps. 编辑:如果有帮助,则该页面不托管在域上。

Edit2: So, I opened Google Chrome without --disable-web-security and I get this error again: Edit2:因此,我在没有--disable-web-security的情况下打开了谷歌浏览器,并且再次出现此错误:

XMLHttpRequest cannot load file:///home/bogdan/Scrabble-Online/public/ba-simple-proxy.php?url=http%3A%2F%2Fapi.wordreference.com%2F0.8%2Fff175%2Fjson%2Froen%2FRHE. Origin null is not allowed by Access-Control-Allow-Origin. 

Yet, if I start it on localhost it doesn't show the message. 但是,如果我在localhost上启动它,则不会显示该消息。 It just doesn't connect. 它只是没有连接。 I am severely confused now. 我现在很困惑。

To make a cross side request your target page needs to allow you to access: 要发出交叉请求,您的目标页面需要允许您访问:

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

You can add this at the top of your proxy script. 您可以将其添加到代理脚本的顶部。 Better replace * with your domain. 最好用您的域替换*

However file:// URLs can't be authorized. 但是, file:// URL不能被授权。 So you need to have a real domain. 因此,您需要拥有一个真实的领域。

Also see https://stackoverflow.com/a/3744697/956397 另请参阅https://stackoverflow.com/a/3744697/956397

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

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