跨域 $http 请求 AngularJS [英] Cross-domain $http request AngularJS

查看:37
本文介绍了跨域 $http 请求 AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 AngularJS 构建的简单网站,它为 json 数据调用 API.

I have simple website I'm building with AngularJS which calls an API for json data.

但是我遇到了跨域源问题,是否有任何方法可以允许跨域请求?

However I am getting Cross domain origin problem is there anyway around this to allow for cross domain requests?

错误:

XMLHttpRequest 无法加载 http://api.nestoria.co.uk/api?country=uk&pretty=1&action=search_listings&place_name=soho&encoding=json&listing_type=rent.请求的资源上不存在Access-Control-Allow-Origin"标头.因此,不允许访问 Origin 'http://localhost'.

XMLHttpRequest cannot load http://api.nestoria.co.uk/api?country=uk&pretty=1&action=search_listings&place_name=soho&encoding=json&listing_type=rent. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:// localhost' is therefore not allowed access.

  searchByPlaceName: function() { 
        var url = baseurl+'country=uk&pretty=1&action=search_listings&place_name=london'+encoding+type;
         return $http.get(url);
    }

推荐答案

api.nestoria.co.uk 似乎不允许 CORS.它必须自己设置 Access-Control-Allow-Origin 标头 - 您无法直接控制它.

It seems that api.nestoria.co.uk does not allow CORS. It has to set the Access-Control-Allow-Origin header itself -- you have no direct control over that.

但是,您可以使用 JSONP.该站点通过 callback 查询参数允许它.

However, you can use JSONP. That site allows it via the callback query parameter.

$http.jsonp(baseurl+'country=uk&pretty=1&action=search_listings&place_name=london'
    +encoding+type + "&callback=JSON_CALLBACK")

这篇关于跨域 $http 请求 AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆