Javascript 在java脚本中获取文件内容

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

Get file contents in java script

javascript

提问by revo

I've a URL that its contents are also JavaScript codes, I want to get the contents of this URL via JavaScript something like to file_get_contents()in PHP and show them to the user using the alert()function.

我有一个 URL,它的内容也是 JavaScript 代码,我想通过 JavaScript 获取这个 URL 的内容,就像file_get_contents()在 PHP 中一样,并使用该alert()函数将它们显示给用户。

回答by Florian Margaine

You can use XMLHttpRequest in Javascript to fetch datas from an external URL. Here is a good resource : http://www.w3schools.com/ajax/default.asp(I usually don't recommend them, but the AJAX tutorial is really good).

您可以在 Javascript 中使用 XMLHttpRequest 从外部 URL 获取数据。这是一个很好的资源:http: //www.w3schools.com/ajax/default.asp(我通常不推荐它们,但 AJAX 教程非常好)。

Also, this had to be there :

此外,这必须在那里:

jQuery ownz

jQuery 拥有

回答by balaphp

you can get the contents from that URL by using jQuery

您可以使用 jQuery 从该 URL 获取内容

$.get('website_url', function(data) {
     alert(data);
});