问题

在某域名下使用Ajax向另一个域名下的页面请求数据,会遇到跨域问题。另一个域名必须在response中添加 Access-Control-Allow-Origin 的header,才能让前者成功拿到数据。

这句话对吗?如果对,那么流程是什么样的?

跨域

怎样才能算跨域?协议,域名,端口都必须相同,才算在同一个域。

当跨域访问时,浏览器会发请求吗

这是真正困扰我们的问题,因为我们不清楚浏览器会怎么做。它会不会检查到你要请求的地址不是同一个域的,直接就禁止了呢? 浏览器确实发出了请求

我做了一个实验,

前端(html)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ajax跨域</title>
<script src="https://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
url:'http://span.home.com/test.php',
type:'POST',
data:'name=hello',
dataType:'json',
success:function(data){
alert(data.name);
}
});
});
});
</script>
</head>
<body> <div id="div1"><h2>使用 jQuery AJAX 获取内容</h2></div>
<button>test</button> </body>
</html>

后台(php)

<?php
header('content-type:application:json;charset=utf8'); $name = isset($_REQUEST['name']) ? $_REQUEST['name'] : 'will';
$arr = array('id'=>1, 'name'=>$name); echo json_encode($arr);

控制台上会打出:

 XMLHttpRequest cannot load http://span.home.com/test.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://span.front.com' is therefore not allowed access. 

Access-Control-Allow-Origin

现在该 Access-Control-Allow-Origin 出场了。只有当目标页面的response中,包含了 Access-Control-Allow-Origin 这个header,并且它的值里有我们自己的域名时,浏览器才允许我们拿到它页面的数据进行下一步处理。如:

Access-Control-Allow-Origin:http://span.front.com

如果它的值设为 * ,则表示谁都可以用:

Access-Control-Allow-Origin: *

后台代码作了部分修改之后就可以正常拿到页面的数据:
<?php
header('content-type:application:json;charset=utf8'); $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
$allow_domain = array('http://span.front.com'); if (in_array($origin, $allow_domain))
{
//Access-Control-Allow-Origin:* 表示允许任何域名跨域访问
header('Access-Control-Allow-Origin:'.$origin);
header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
}
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : 'will';
$arr = array('id'=>1, 'name'=>$name); echo json_encode($arr);

注:前端域名 http://span.front.com, 后台域名 http://span.home.com

 

ajax跨域(No 'Access-Control-Allow-Origin' header is present on the requested resource)的更多相关文章

  1. Webapi 跨域 解决解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource 问题

    首先是web端(http://localhost:53784) 请求 api(http://localhost:81/api/)时出现错误信息: 查看控制台会发现错误:XMLHttpRequest c ...

  2. SpringBoot添加Cors跨域配置,解决No 'Access-Control-Allow-Origin' header is present on the requested resource

    目录 什么是CORS SpringBoot 全局配置CORS 拦截器处理预检请求 什么是CORS 跨域(CORS)请求:同源策略/SOP(Same origin policy)是一种约定,由Netsc ...

  3. As.net WebAPI CORS, 开启跨源访问,解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource

    默认情况下ajax请求是有同源策略,限制了不同域请求的响应. 例子:http://localhost:23160/HtmlPage.html 请求不同源API http://localhost:228 ...

  4. java、ajax 跨域请求解决方案('Access-Control-Allow-Origin' header is present on the requested resource. Origin '请求源' is therefore not allowed access.)

      1.情景展示 ajax调取java服务器请求报错 报错信息如下: 'Access-Control-Allow-Origin' header is present on the requested ...

  5. 跨域问题解决----NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost:11000' is therfore not allowed access'

    NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost ...

  6. .Net Core 处理跨域问题Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

    网页请求报错: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Or ...

  7. (转)AJax跨域:No 'Access-Control-Allow-Origin' header is present on the requested resource

    在本地用ajax跨域访问请求时报错: No 'Access-Control-Allow-Origin' header is present on the requested resource. Ori ...

  8. js跨域访问,No 'Access-Control-Allow-Origin' header is present on the requested resource

    js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is prese ...

  9. WCF REST开启Cors 解决 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405.

    现象: 编写了REST接口: [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(UriTemp ...

随机推荐

  1. Cassandra标准列和超级列

    列(column)是Cassandra数据模型中的最基本的数据结构单元.列是一个由列名(key).值(value).时间戳(timestamp)构成的三元组.在关系型数据库中,你需要先定义列的名称和和 ...

  2. js 手机号码和电话号码正则校验

    checkPhone() { var mobile = ''; var tel = /^0\d{2,3}-?\d{7,8}$/; var phone = /^(((13[0-9]{1})|(15[0- ...

  3. Miniconda安装scrapy教程

    一.背景说明 前两天想重新研究下Scrapy,当时的环境是PyCharm社区版+Python 3.7.使用pip安装一直报错 “distutils.errors.DistutilsPlatformEr ...

  4. openssh安装/更新教程(CentOS)

    由于rpm包版本总落后于tar包,对于想安装新版本或由于漏洞需要更新到新版本那只能选择源代方式编译安装. 更新执行和安装一样的步骤就行了. 1.下载 官方网址:http://www.openssh.c ...

  5. 怎么样才是设计功能函数的好思路(javascript)?

    在js里面,对于函数的调用,实际上也是也是面向对象的思路,于是写好js函数,也是考核面向对象设计的能力,同时也必须考虑到如何实现高内聚和低耦合,拿一个例子来说,现在的需求是这样的,实现个投资进度框,就 ...

  6. 研究Duilib的实现结构

    第一步:调用RegisterClassEx(): 第二步:调用CreateWindowEx() 第三步:调用ShowWindow(),同时启动消息循环 说明: 1.RegisterClass,Regi ...

  7. POJ 3436 ACM Computer Factory 最大流,拆点 难度:1

    题目 http://poj.org/problem?id=3436 题意 有一条生产线,生产的产品共有p个(p<=10)零件,生产线上共有n台(n<=50)机器,每台机器可以每小时加工Qi ...

  8. wordpress有用的插件

    代码高亮 https://wordpress.org/plugins/crayon-syntax-highlighter/ 这个貌似比较好用 百度UEditor(富文本编辑器) https://www ...

  9. CentOS7下安装MySQL5.7安装与配置

    介绍在CentOS7上yum安装数据库服务器MySQL Community Server 5.7的方法. 准备 CentOS7默认安装了和MySQL有兼容性的MariaDB数据库,在我们安装MySQL ...

  10. Java多线程习题 ===重点 ,错题积累

    多线程重点,错题分析 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: . 12: 13: 14: 15: