angular自定义跨域服务器,angular get请求跨域问题?如何通过本地项目跨域获取服务器上的数据?...

我在本地有一个angular的项目文件。

我在run运行块里面写了个$http:

app.run(function ($rootScope, $http) {

$http({

method: 'GET',

url: 'http://xxx/data/count.php',

params: { 'mode': 'nor' }

}).success(function (response) {

console.log(response)

})

});

然后一开始我在网上查询了一下失败的原因,在服务器的php接口文件上加上了header

header("Access-Control-Allow-Origin: *");

可是错误依然:

XMLHttpRequest cannot load http://xxx/data/count.php?mode=nor. Request header field Pragma is not allowed by Access-Control-Allow-Headers in preflight response.

请问如何才能让本地的angular代码能够获取到远端数据?

附上php接口文件代码:

<?php

header('Access-Control-Allow-Origin:*');

$list=num_it($_GET["site"]);

$res=get_son_where($list);

$res=array_iconv("gb2312","utf-8//IGNORE",$res);

$result=["collist"=>$res];

echo json_encode($result);

//$result有内容

?>

header('Access-Control-Allow-Origin: http://xxx');

这里不要写

*

,最好写完整的

http://xxx

,因为

Access-Control-Allow-Headers

在跨域的涉及的数据交换时必须要严格指定来源。

然后浏览器端需要交互cookie等的话,还需要js中加

withCredentials: true

$http({

method: 'GET',

url: 'http://xxx/data/count.php',

withCredentials: true,

params: { 'mode': 'nor' }

})

利用 Interceptors 来统一处理 HTTP 的错误

3431402c1bcafac7e7cfb291f3f55629.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值