javascript 中的 file_Get_contents

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4583059/
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-10-25 14:12:03  来源:igfitidea点击:

file_Get_contents in javascript

phpjavascriptmysqlphpjs

提问by Speedy Wap

i want to get data from other sites using javascript executed from my website.

我想使用从我的网站执行的 javascript 从其他网站获取数据。

回答by pritaeas

The PHPJSwebsite has some nice conversions of PHP functions into Javascript.

PHPJS网站有PHP函数一些很好的转换为JavaScript。

回答by Quentin

In general, unless they expose the data with JSON-P, you can't thanks to the security considerations imposed by the same origin policy.

一般来说,除非他们使用JSON-P公开数据,否则您不能感谢同源策略强加的安全考虑。

Recent browsers support a permissions systemwhere a remote site can allow JavaScript running on a remote site to make a request. Flash provides a similar system, so can act as an intermediary. Both of these require the cooperation of the remote site.

最近的浏览器支持权限系统,其中远程站点可以允许在远程站点上运行的 JavaScript 发出请求。Flash 提供了类似的系统,因此可以充当中介。这两者都需要远程站点的配合。

The usual work around is to use a proxy service, either running on your own system (so JS makes the request to the same server, which fetches the data from the remote site) or a third-party service like YQL.

通常的解决方法是使用代理服务,要么在您自己的系统上运行(因此 JS 向同一服务器发出请求,后者从远程站点获取数据)或第三方服务,如YQL

回答by Klemen Slavi?

Javascript is limited by the same-domain security policy. The only way to get data from other sites is to use JSONPor build a proxy on your own host that lets you curlcontent from other sites.

Javascript 受同域安全策略限制。从其他站点获取数据的唯一方法是使用JSONP或在您自己的主机上构建代理,以便您curl从其他站点获取内容。

回答by jerluc

That really depends on what you mean by "data". Try using AJAX if its just for simple requests.

这真的取决于你所说的“数据”是什么意思。如果仅用于简单请求,请尝试使用 AJAX。

回答by St.Woland

Use jQuery:

使用jQuery:

$.post( 'http://some.website.com/file.js', function(result){
    alert(result);
});

You may not fetch anything but JavaScript or JSON.

除了 JavaScript 或 JSON,您可能无法获取任何内容。

Or try this answer: How do I send a cross-domain POST request via JavaScript?

或者试试这个答案:如何通过 JavaScript 发送跨域 POST 请求?

回答by Ben

It has to be done server side - send an ajax request, run the PHP you want, and check the responseTextproperty to see the results.

它必须在服务器端完成 - 发送一个ajax 请求,运行你想要的 PHP,然后检查responseText属性以查看结果。