获得错误“请求的资源上不存在“Access-Control-Allow-Origin”标头。 in ionic2 [英] Getting Error " No 'Access-Control-Allow-Origin' header is present on the requested resource." in ionic2

查看:3749
本文介绍了获得错误“请求的资源上不存在“Access-Control-Allow-Origin”标头。 in ionic2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在运行于localhost:8100上的Ionic2应用程序中 localhost:58682 / home / index1 上运行的WebApi来获取数据。

我得到以下内容错误消息。

I am trying to fetch data using WebApi which runs on localhost:58682/home/index1 in Ionic2 app which runs on localhost:8100.
I am getting the following error message.

XMLHttpRequest cannot load http://localhost:58682/home/index1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

我的 service.ts 文件

 @Injectable()
export class ResponseService{
 
 private responseUrl='http://localhost:58682/home/index1';
   getresponseDetails()
    {
        return this.http.get(this.responseUrl).map(res=>res.json());
    }
    }

ionic.config.json

{
  "name": "App",
  "app_id": "90594fdf",
  "type": "ionic-angular",
  "proxies":[
    {
      "path":"/home",
      "proxyUrl":"http://localhost:58682/home"
    }
  ]
}

我浏览了一些CORS博客但无法修复它。究竟出了什么问题?

I went through some CORS blogs but couldn't fix it. What exactly is going wrong?

推荐答案

使用此插件在您的浏览器中。

它允许您请求任何来自任何来源的ajax的网站,无论http / https差异等。实际上在响应中添加了 Allow-Control-Allow-Origin:* 标题。

It allows to you request any site with ajax from any source, no matter http/https discrepancies and the like. Practically adds the Allow-Control-Allow-Origin: * header in the response.

请记住这是一种创可贴解决方案。您的服务器响应必须实际上具有'Access-Control-Allow-Origin':'*'标头,最好具有比更具体的值*

Please keep in mind that this is a band-aid solution. Your server response has to actually have the 'Access-Control-Allow-Origin': '*' header, preferably with a more specific value than *.

这篇关于获得错误“请求的资源上不存在“Access-Control-Allow-Origin”标头。 in ionic2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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