angular的http请求本地文件出现跨域提醒
来源:互联网 发布:金九银十原油利多数据 编辑:程序博客网 时间:2024/05/22 13:02
用angular测试请求本地的json数据,请求出错,提示跨域,我的代码如下:
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>angular使用http请求数据</title> <script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script></head><body ng-app="app" ng-controller="testCtrl"> <h1>angular使用http请求数据</h1> 请求结果:{{resultData}}<script> var module=angular.module("app",[]); module.controller("testCtrl",function($scope,$http){ $http({ method:"GET", url:"test.json" }).then(function successCallback(res){ $scope.resultData=res.data; }).then(function errorCallback(res){ $scope.resultData="请求出错啦"; }); });</script></body></html>
test.json代码如下:
{ "sites": [ { "Name": "Google", "Url": "www.google.com", "Country": "USA" }, { "Name": "Facebook", "Url": "www.facebook.com", "Country": "USA" }, { "Name": "微博", "Url": "www.weibo.com", "Country": "CN" } ]}原因:因为浏览器为了出于安全考虑把请求本地视为跨域请求。
解决办法:
第一步:关闭谷歌浏览器;
第二步:ctrl+r输入cmd进入控制台,输入如下命令:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files
注意:一定要先关闭浏览器才生效哦;然后再打开html文件就可以获取到数据了。
阅读全文
1 0
- angular的http请求本地文件出现跨域提醒
- $http——ajax跨域请求的angular方式
- Angular开发(十四)-利用angular的http转发、即代理http 请求,处理项目中请求跨域的问题
- angular的http请求方式
- angular的http三种请求方式
- Angular学习:http请求失败的问题
- Angular----$http请求数据
- http请求本地文件,路径中含有空格的处理 QT
- Angular 的 $http 跨域的问题
- angular的$http的ajax的请求操作
- angular用$http请求数据
- angular中的http请求封装
- angular的http请求需要注意的地方
- angular 取消或终止$http的ajax请求
- Angular开发(十六)-HTTP请求的详细介绍
- angular跨域的ajax--json数据请求
- r.js压缩angular项目,Angular+requirejs+rjs实现初始化http请求数的优化
- angular与SpringMVC项目 跨域请求
- Swift 枚举
- 小程序如何快速获得3000用户?
- 在Android中如何使用clipPath()方法实现简单的裁剪圆形图片
- 代码示例:通过Python直接解析xml并转换为json,保存json语句文件再加载解析json
- python urllib2 操作
- angular的http请求本地文件出现跨域提醒
- tensorflow 学习2--- mnist测试
- Day14-44.Initialization & Cleanup
- 面试
- CAP理论十二年回顾:"规则"变了
- spring mvc4:异常处理
- CentOS 7 修改时区方法
- 应用开发的先备概念、流程
- linux 将一个服务器上的文件或者文件夹复制到另一台服务器上