您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Ajax怎么设置实现跨域访问

2024/6/9 10:16:50发布29次查看
这次给大家带来ajax怎么设置实现跨域访问,ajax设置实现跨域访问的注意事项有哪些,下面就是实战案例,一起来看一下。
ajax跨域访问是一个老问题了,解决方法很多,比较常用的是jsonp方法,jsonp方法是一种非官方方法,而且这种方法只支持get方式,不如post方式安全。
即使使用jquery的jsonp方法,type设为post,也会自动变为get。
官方问题说明:
“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.note: this will turn posts into gets for remote-domain requests.
如果跨域使用post方式,可以使用创建一个隐藏的iframe来实现,与ajax上传图片原理一样,但这样会比较麻烦。
因此,通过设置access-control-allow-origin来实现跨域访问比较简单。
例如:客户端的域名是www.client.com,而请求的域名是www.server.com
如果直接使用ajax访问,会有以下错误
xmlhttprequest cannot load http://www.server.com/server.php. no 'access-control-allow-origin' header is present on the requested resource.origin 'http://www.client.com' is therefore not allowed access.
在被请求的response header中加入
// 指定允许其他域名访问  header('access-control-allow-origin:*');  // 响应类型  header('access-control-allow-methods:post');  // 响应头设置  header('access-control-allow-headers:x-requested-with,content-type');
就可以实现ajax post跨域访问了。
代码如下:
client.html 路径:http://www.client.com/client.html
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">  <html>   <head>   <meta http-equiv="content-type" content="text/html;charset=utf-8">   <title> 跨域测试 </title>   <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>   </head>   <body>   <p id="show"></p>   <script type="text/javascript">   $.post(http://www.server.com/server.php,{name:fdipzone,gender:male})   .done(function(data){   document.getelementbyid(show).innerhtml = data.name + ' ' + data.gender;   });   </script>   </body>  </html>
server.php 路径:http://www.server.com/server.php
<?php $ret = array( 'name' => isset($_post['name'])? $_post['name'] : '',   'gender' => isset($_post['gender'])? $_post['gender'] : ''  );  header('content-type:application:json;charset=utf8');  header('access-control-allow-origin:*');  header('access-control-allow-methods:post');  header('access-control-allow-headers:x-requested-with,content-type');  echo json_encode($ret);  ?>
access-control-allow-origin:* 表示允许任何域名跨域访问
如果需要指定某域名才允许跨域访问,只需把access-control-allow-origin:*改为access-control-allow-origin:允许的域名
例如:header('access-control-allow-origin:http://www.client.com');
如果需要设置多个域名允许访问,这里需要用php处理一下
例如允许 www.client.com 与 www.client2.com 可以跨域访问
server.php 修改为
<?php $ret = array( 'name' => isset($_post['name'])? $_post['name'] : '',   'gender' => isset($_post['gender'])? $_post['gender'] : ''  );  header('content-type:application:json;charset=utf8');  $origin = isset($_server['http_origin'])? $_server['http_origin'] : '';  $allow_origin = array(   'http://www.client.com',   'http://www.client2.com'  );  if(in_array($origin, $allow_origin)){   header('access-control-allow-origin:'.$origin);   header('access-control-allow-methods:post');   header('access-control-allow-headers:x-requested-with,content-type');  }  echo json_encode($ret);  ?>
源码下载地址:http://xiazai.jb51.net/201702/yuanma/demo(jb51.net)
下面是其他网友的补充:
最近在用cocos2d-js做游戏时,
在本地用ajax跨域访问请求时报错:
xmlhttprequest cannot loadhttp://www.zjblogs.com/. no 'access-control-allow-origin' header is present on the requested resource. origin 'null' is therefore not allowed access.
在网上查了下,解决办法如下:
1、如果请求的url是aspx页面,则需要在aspx页面中添加代码:response.addheader(access-control-allow-origin, *);
2、如果请求的url是php页面,则需要在php页面中添加代码:header(access-control-allow-origin: *);
3、如果请求的url是静态的html页面,则需要在页面中添加meta标签代码:
这里的*是代表允许所有域名访问,如果服务器端可以确定是要被哪些域名访问,最好是能把以上代码中的“*”代替为具体的域名,这样做可以相应的增强安全性。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
用ajax实现注册与头像上传功能
ajax与$.ajax的使用方法
以上就是ajax怎么设置实现跨域访问的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product