一、什么是跨域访问

举个栗子:在A网站中,我们希望使用Ajax来获得B网站中的特定内容。如果A网站与B网站不在同一个域中,那么就出现了跨域访问问题。你可以理解为两个域名之间不能跨过域名来发送请求或者请求数据,否则就是不安全的。跨域访问违反了同源策略,同源策略的详细信息可以点击如下链接:Same-origin_policy; 
总而言之,同源策略规定,浏览器的ajax只能访问跟它的HTML页面同源(相同域名或IP)的资源。

二、解决方案

常用的解决方案有两种,可以分为客户端解决方案和服务器端解决方案。先说服务器端解决方案:

  • 服务器端解决方案 
    在服务器端的filter或者servlet里面添加 
    response.setHeader("Access-Control-Allow-Origin", "*"); 
    “Access-Control-Allow-Origin”表示允许跨域访问,“*”表示允许所有来源进行跨域访问,这里也可以替换为特定的域名或ip。 
    很显然,这种方式对非网站拥有人员来说是不能做到的。而且此种方式很容易受到CSRF攻击。

  • 客户端解决方案

$(function($){
var url = 'http://*****/index';
$.ajax(url, {
data: {
'cityname': '成都',
'date': '2016.12.12'
},
dataType: 'jsonp',
crossDomain: true,
success: function(data) {
if(data && data.resultcode == '200'){
console.log(data.result.today);
}
}
});

将ajax请求中的dataType属性设置为“jsonp”,jsonp是专门用来解决跨域访问而诞生的。

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.的更多相关文章

  1. 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 ...

  2. Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fromHere.com' is therefore not allowed access.

    Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is presen ...

  3. 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 ...

  4. 跨域问题解决----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 ...

  5. XMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' ' is therefore not allowed access.

    ajax跨域 禁止访问! 利用Access-Control-Allow-Origin响应头解决跨域请求

  6. 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 ...

  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. No 'Access-Control-Allow-Origin' header is present on the requested resource.

    今天做一个AJAX案例时,浏览器监控到如下错误: XMLHttpRequest cannot load http://54.169.69.60:8081/process_message. No 'Ac ...

  9. ajax请求node.js接口时出现 No 'Access-Control-Allow-Origin' header is present on the requested resource错误

    ajax请求node.js接口出现了如下的错误: XMLHttpRequest cannot load http://xxx.xxx.xx.xx:8888/getTem?cityId=110105&a ...

随机推荐

  1. docker rmi 详解

    docker rmi 用于删除指定的镜像,常见用法如下: [root@localhost ~]$ docker rmi centos # 根据 REPOSITORY 来删除镜像 [root@local ...

  2. vue2.0+element-ui(01简单点的单页面)

    前言: 在<Vue.js权威指南>刚出版的时候,自己就作为一名前端粉捧了一把场,可是真是应了那句“出来混,总是要还的“这句话了,那时候信心满满的买来书想要认真研究,最终却还是把它搁浅了.直 ...

  3. Spring学习笔记--Spring表达式语言SpEL

    Spring3引入了Spring表达式语言(Spring Expression Language,SpEL).SpEL是一种强大的.简洁的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean ...

  4. Xcode模版生成文件头部注释

    在使用Xcode创建工程或者新建类的时候,顶部都会有一些xcode帮我们生成的注释 //// MySingletonClass.h// 单例模式//// Created by mark on 15/8 ...

  5. 如何打开或关闭windows的测试模式

    百度经验:jingyan.baidu.com windows的测试模式就如同字面意思一样,是一个测试用的模式.这个模式的标志主要在有非官方驱动或系统关键文件运行时显示.例如安装了大内存补丁(32位系统 ...

  6. (1.1.6)UVA 10978 Let's Play Magic!(直叙式模拟)

    /* * UVA_10978.CPP * * Created on: 2013年10月6日 * Author: Administrator */ #include <iostream> # ...

  7. java高级---->Thread之FutureTask的使用

    FutureTask类是Future 的一个实现,并实现了Runnable,所以可通过Excutor(线程池) 来执行,也可传递给Thread对象执行.今天我们通过实例来学习一下FutureTask的 ...

  8. springdata----->spring集成redis(一)

    使用spring-data-redis与redis集成,今天我们就通过例子来学习一下.当时间和耐心都已经变为奢侈,我们只能靠星座了解彼此. spring与redis集成的实例 注意:这里我们测试的是安 ...

  9. LeetCode——Happy Number

    Description: Write an algorithm to determine if a number is "happy". A happy number is a n ...

  10. Ubuntu16.04安装Elasticsearch

    一.安装工作 wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - sudo apt-g ...