通过使用PHP和JavaScript CORS实现站点间的跨域通信 [英] Implementing cross domain communication between sites through CORS using PHP and Javascript

查看:121
本文介绍了通过使用PHP和JavaScript CORS实现站点间的跨域通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览Web和尝试一切可能的事情的时候,我就知道有我的主机服务器的问题。它是由我的主机服务器不可用。所以,我以为张贴在我的#1 code,这样,没有人对SO失去的时间,像我一样。

After days of browsing on web and trying every possible thing, I came to know about there were issues with my host server. It was disabled by my host server. So, I thought of posting my code on Stackoverflow, so that , no one else on SO loses time as I did.

在code段工作完全正常的IE浏览器,Safari浏览器,Mozilla和Chrome浏览器。

The code snippet works perfectly fine on IE, Safari, Mozilla and Chrome.

推荐答案

Javascript的code的客户端

Javascript Code for the client side

<script type='text/javascript'>

// function for making an object for making AJAX request

function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}

var http899 = getXMLHTTPRequest();
function searchFabIndia() {

var myurl = "http://my2nddomain.com/yebhi.php";
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl+"?rand="+myRand;
http899.open("GET", modurl, true);
http899.onreadystatechange = useHttpResponse899;
http899.send(null);
}

function useHttpResponse899() {
if (http899.readyState == 4) {
if(http899.status == 200) {
 // do all processings with the obtained values / response here
}
}
}

</script>

<body onload='searchFabIndia();'>

在服务器端所需要的code部分。您需要设置的来源(引荐)谁可以要求页面内容,让方法和标头。这些设置可以存储在.htaccess文件一起二号域中的所有文件,您正在请求,或者,你可以把它们放在特定的PHP文件,如下所示: -

Part of the code required on the server side. You need to set the origin(referrers) who can ask for the page content, allow methods and headers. These settings can be stored either in the .htaccess file together for all the files on the 2nd domain to which you are making request, or, you can put them in your specific PHP file as shown:-

    <?php
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        header("Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");
        header("Access-Control-Max-Age: 18000");
// Put it in your PHP file
    ?>

否则,您可以如图在一起提相同的.htaccess文件中指定这些设置对整个域/子域: -

OR else , you can specify these settings for the entire domain/sub-domain together by mentioning the same in your .htaccess file as shown :-

<IfModule mod_headers.c>
   <FilesMatch "\.(php)$">
    Header set Access-Control-Allow-Origin: *
    Header set Access-Control-Allow-Methods : POST,GET,OPTIONS,PUT,DELETE
</FilesMatch>
  </IfModule>

另外不说,通配符津贴所有的引荐可有时不必要的,所以,在这种情况下,你可以通过命名来指定特定的域/子域,他们每个人分开用逗号(,),如图

Also not that the wildcard allowance to all the referrers can be sometimes unnecessary , so, in that case you can specify the specific domain/sub-domain by naming them , each of them separated by comma(,) as shown

Header set Access-Control-Allow-Origin: http://abc.com,http://def.com,http://ghi.com 

请万一你遇到在实施任何这些有些难度发表评论。你可以看到我所提到的现场演示,这里

Please comment in case you face some difficulty in implementing any of these. You can see live demo of what I mentioned here

这篇关于通过使用PHP和JavaScript CORS实现站点间的跨域通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆