ajax跨域(No 'Access-Control-Allow-Origin' header is present on the requested resource)
问题
在某域名下使用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)的更多相关文章
- Webapi 跨域 解决解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource 问题
首先是web端(http://localhost:53784) 请求 api(http://localhost:81/api/)时出现错误信息: 查看控制台会发现错误:XMLHttpRequest c ...
- SpringBoot添加Cors跨域配置,解决No 'Access-Control-Allow-Origin' header is present on the requested resource
目录 什么是CORS SpringBoot 全局配置CORS 拦截器处理预检请求 什么是CORS 跨域(CORS)请求:同源策略/SOP(Same origin policy)是一种约定,由Netsc ...
- 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 ...
- 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 ...
- 跨域问题解决----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 ...
- .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 ...
- (转)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 ...
- 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 ...
- 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 ...
随机推荐
- iBeacon室内定位原理解析【转】
目前,技术发展持续火热,因着iBeacon的定位精度和造价都比较符合国内室内定位的市场需求,下面我们来聊一聊iBeacon室内定位原理. iBeacon定位原理 iBeacon是一项低耗能蓝牙技术,工 ...
- 通过DBMS_REDEFINITION包对表在线重定义
基础介绍 Oracle Online Redefinition可以保证在数据表进行DDL类型操作,如插入.删除数据列,分区处理的时候,还能够支持DML操作,特别是insert/update/delet ...
- Python爬虫有道翻译接口
import urllib.request import urllib.parse import json import hashlib from datetime import datetime i ...
- ActiveMQ使用例子
网上收集的例子:有broker,producer,consumer public class MqApp { public static void main(String[] args) throws ...
- FastDFS安装教程
1.下载 FastDFS下载:https://codeload.github.com/happyfish100/fastdfs/zip/master 库文件下载:https://codeload.gi ...
- 在eclipse激活maven profile配置
profile简介 profile可以让我们定义一系列的配置信息,然后指定其激活条件.这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同 ...
- forget sus,syn sym semi word out~s
1★ sus 在~下面 2★ syn 3★ sym 共同 4★ semi 半
- ubuntu16.10 中安装mysql
1.安装MYSQL: root@ubuntu:~# sudo apt-get install mysql-server root@ubuntu:~# apt isntall mysql-client ...
- 逆袭之旅.DAY07东软实训..封装~继承~抽象~final
2018年7月3日.逆袭之旅DAY07 package day0703.exam1; /** * 狗狗类 使用权限修饰符private和public进行封装 * @author Administrat ...
- mips编译器交叉编译openssl
1.下载源码: git clone https://github.com/openssl/openssl.git 2. 配置生成Makefile ./config no-asm shared --p ...