ajax 设置Access-Control-Allow-Origin实现跨域访问

      发布在:个人笔记      评论:0 条评论
<p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">ajax跨域访问是一个老问题了,解决方法很多,比较常用的是<span style="color: rgb(255, 0, 0);">JSONP方法,JSONP方法是一种非官方方法,而且这种方法只支持GET方式,不如POST方式安全。</span></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">即使使用jquery的jsonp方法,type设为POST,也会自动变为GET。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">官方问题说明:<br/></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">“script”: Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, “_=[TIMESTAMP]“, to the URL unless the cache option is set to true.<span style="color: rgb(255, 0, 0);">Note: This will turn POSTs into GETs for remote-domain requests.</span></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">如果跨域使用POST方式,可以使用创建一个隐藏的iframe来实现,与ajax上传图片原理一样,但这样会比较麻烦。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong>因此,通过设置<span style="color: rgb(255, 0, 0);">Access-Control-Allow-Origin来实现跨域访问比较简单。</span></strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">例如:客户端的域名是<strong>www.client.com</strong>,而请求的域名是<strong>www.server.com</strong><br/></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">如果直接使用ajax访问,会有以下错误<br/></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">XMLHttpRequest cannot load http://www.server.com/server.php. No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource.<span style="color: rgb(255, 0, 0);">Origin &#39;http://www.client.com&#39; is therefore not allowed access.</span></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong>在被请求的Response header中加入</strong></p><pre class="brush:as3;toolbar:false"> // 指定允许其他域名访问 原来以为设置这个就可以了,原来缺少下面2,3两行代码 header(&#39;Access-Control-Allow-Origin:*&#39;); <br/> <br/> <br/> // 响应类型 <br/> header(&#39;Access-Control-Allow-Methods:POST&#39;); // 响应头设置 header(&#39;Access-Control-Allow-Headers:x-requested-with,content-type&#39;); <br/></pre><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);">就可以实现ajax POST跨域访问了。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong>代码如下:</strong><br/></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong>client.html</strong>&nbsp;路径:<a href="http://www.client.com/client.html">http://www.client.com/client.html</a></p><pre class="brush:as3;toolbar:false"> &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html;charset=utf-8&quot;&gt; &lt;title&gt; 跨域测试 &lt;/title&gt; &lt;script src=&quot;//code.jquery.com/jquery-1.11.3.min.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id=&quot;show&quot;&gt;&lt;/div&gt; &lt;script type=&quot;text/javascript&quot;&gt; $.post(&quot;http://www.server.com/server.php&quot;,{name:&quot;fdipzone&quot;,gender:&quot;male&quot;}) .done(function(data){ document.getElementById(&quot;show&quot;).innerHTML = data.name + &#39; &#39; + data.gender; }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; <br/></pre><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong>server.php</strong>&nbsp;路径:http://www.server.com/server.php</p><pre class="brush:as3;toolbar:false"> &lt;?php $ret = array( &#39;name&#39; =&gt; isset($_POST[&#39;name&#39;])? $_POST[&#39;name&#39;] : &#39;&#39;, &#39;gender&#39; =&gt; isset($_POST[&#39;gender&#39;])? $_POST[&#39;gender&#39;] : &#39;&#39; ); header(&#39;content-type:application:json;charset=utf8&#39;); header(&#39;Access-Control-Allow-Origin:*&#39;); header(&#39;Access-Control-Allow-Methods:POST&#39;); header(&#39;Access-Control-Allow-Headers:x-requested-with,content-type&#39;); echo json_encode($ret); ?&gt; <br/></pre><pre class="brush:php;toolbar:false">Access-Control-Allow-Origin:* 表示允许任何域名跨域访问 如果需要指定某域名才允许跨域访问,只需把Access-Control-Allow-Origin:*改为Access-Control-Allow-Origin:允许的域名 例如:header(&#39;Access-Control-Allow-Origin:http://www.client.com&#39;); 如果需要设置多个域名允许访问,这里需要用php处理一下 例如允许 www.client.com 与 www.client2.com 可以跨域访问 server.php 修改为<br/><br/> &lt;?php $ret = array( &#39;name&#39; =&gt; isset($_POST[&#39;name&#39;])? $_POST[&#39;name&#39;] : &#39;&#39;, &#39;gender&#39; =&gt; isset($_POST[&#39;gender&#39;])? $_POST[&#39;gender&#39;] : &#39;&#39; ); header(&#39;content-type:application:json;charset=utf8&#39;); $origin = isset($_SERVER[&#39;HTTP_ORIGIN&#39;])? $_SERVER[&#39;HTTP_ORIGIN&#39;] : &#39;&#39;; $allow_origin = array( &#39;http://www.client.com&#39;, &#39;http://www.client2.com&#39; ); if(in_array($origin, $allow_origin)){ header(&#39;Access-Control-Allow-Origin:&#39;.$origin); header(&#39;Access-Control-Allow-Methods:POST&#39;); header(&#39;Access-Control-Allow-Headers:x-requested-with,content-type&#39;); } echo json_encode($ret); ?&gt; <br/></pre><p>来源:https://www.cnblogs.com/wawahaha/p/4799686.html</p>
相关文章
热门推荐