jQuery getJSON 到外部 PHP 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/790910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:51:13  来源:igfitidea点击:

jQuery getJSON to external PHP page

phpjqueryajaxjsongetjson

提问by Pmarcoen

I've been trying to make an AJAX request to an external server. I've learned so far that I need to use getJSON to do this because of security reasons ?

我一直在尝试向外部服务器发出 AJAX 请求。到目前为止,我已经了解到出于安全原因我需要使用 getJSON 来执行此操作?

Now, I can't seem to make a simple call to an external page. I've tried to simplify it down as much as I can but it's still not working. I have 2 files, test.html & test.php

现在,我似乎无法对外部页面进行简单调用。我试图尽可能地简化它,但它仍然不起作用。我有 2 个文件,test.html 和 test.php

my test.html makes a call like this, to localhost for testing :

我的 test.html 像这样调用本地主机进行测试:

    $.getJSON("http://localhost/OutVoice/services/test.php", function(json){
    alert("JSON Data: " + json);
});

and I want my test.php to return a simple 'test' :

我希望我的 test.php 返回一个简单的“测试”:

$results = "test";
echo json_encode($results);

I'm probably making some incredible rookie mistake but I can't seem to figure it out. Also, if this works, how can I send data to my test.php page, like you would do like test.php?id=15 ?

我可能犯了一些令人难以置信的菜鸟错误,但我似乎无法弄清楚。另外,如果这有效,我如何将数据发送到我的 test.php 页面,就像你会像 test.php?id=15 那样?



The test.html page is calling the test.php page on localhost, same directory I don't get any errors, just no alert ..

test.html 页面正在调用本地主机上的 test.php 页面,同一目录我没有收到任何错误,只是没有警报..

回答by moff

It couldbe that you haven't got a callback in test.php. Also, json_encodeonly accepts an array:

可能是你没有在test.php的回调。此外,json_encode只接受一个数组:

$results = array("key" => "value");
echo $_GET['callback'] . '(' . json_encode($results) . ')';
// the callback stuff is only needed if you're requesting from different domains

jQuery automatically switches to JSONP (i.e. using script tags instead of XMLHttpRequest) when you use http://. If you have test.html and test.php on the same domain, try using relative paths (and no callbacks).

jQuery将自动切换到JSONP(即使用脚本标记代替XMLHttpRequest)当您使用http://。如果您在同一个域中有 test.html 和 test.php,请尝试使用相对路径(并且没有回调)。

回答by drewish

Be careful with moff's answer. It's got a common XSS vulnerability: http://www.metaltoad.com/blog/using-jsonp-safely

小心 moff 的回答。它有一个常见的 XSS 漏洞:http: //www.metaltoad.com/blog/using-jsonp-safely

回答by Ally

The simplest solution would be to add the below code before any output to your test.php file, then you have more flexibility with what methods you use, a standard ajax call should work.

最简单的解决方案是在 test.php 文件的任何输出之前添加以下代码,然后您可以更灵活地使用哪些方法,标准的 ajax 调用应该可以工作。

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

However, use the json callback thing when your getting data from a server beyond your control.

但是,当您从无法控制的服务器获取数据时,请使用 json 回调。