简体   繁体   English

使用Javascript在页面中包含php文件

[英]include php file in page using Javascript

How do I include php link in page using Javascript 如何使用Javascript在页面中包含php链接

Using only Javascript 仅使用Javascript

I had a code but forgotten it 我有一个密码,但忘记了

ex

This in php 这在PHP

 <? php
 file_get_contents('www.xxxxxxxxx.com/icn.php); 
  ?>

I want in Javascript 我想要用Javascript

Since you are including an external domain in the precess (not using a relative path), the request will probably be cancelled due to the same origin policy browser implements to avoid cross-site forgery. 由于您在进项中包含一个外部域(不使用相对路径),因此该请求可能会被取消,因为浏览器实施了相同的源策略,以避免跨站点伪造。 In that case, you may want to have a PHP file on your website that just proxies your request to the actual site you want to load and use AJAX to your local PHP script. 在这种情况下,您可能希望在您的网站上拥有一个PHP文件,该文件仅将您的请求代理到您要加载的实际站点,并使用AJAX到本地PHP脚本。

Your Javascript <> PHP script on the same domain <> www.xxxxxxxxx.com/icn.php

If www.xxxxxxxxx.com happens to be your own website, you can just do a regular AJAX call to "icn.php". 如果www.xxxxxxxxx.com恰好是您自己的网站,则可以仅对“ icn.php”进行常规AJAX调用。 To do AJAX calls, you may want to read about XMLHttpRequest or use a library that does the job for you, like jQuery.ajax() . 要进行AJAX调用,您可能需要阅读XMLHttpRequest或使用可以为您完成工作的库,例如jQuery.ajax()

If you really absolutely need to load data from an external domain purely on client side, you can either use hacks like JSONP or an invisible <iframe> , listen to its onload event and get its content. 如果确实确实需要纯粹在客户端从外部域加载数据,则可以使用JSONP之类的 hack或不可见的<iframe> ,监听其onload事件并获取其内容。 But be careful, these usually requires the external site to be compatible with these kind of requests (not a big deal if you manage both ends). 但是请注意,这些通常要求外部站点与此类请求兼容(如果您同时管理两端,则没什么大不了的)。

fun.php fun.php

< ?php <?php

echo 'hello World'; 回声“你好世界”;

?> ?>

function foo() { 函数foo(){

var i = < ?php include('fun.php');?> var i = <?php include('fun.php');?>

console.log( i ); console.log(i);

} }

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

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