【问题标题】:Ionic CORS issue with JIRA APIJIRA API 的离子 CORS 问题
【发布时间】:2019-02-22 12:46:17
【问题描述】:

问题描述:我已经构建了一个离子应用程序,它使用 JIRA rest api 来获取问题(GET 数据),创建问题(POST 数据)。我总是收到类似 preflight request did not succeed or same origin policy 的 CORS 错误,当我们使用 ionic serve 时,这是预期的,但在我构建和发布签名的 apk 时同样不起作用。

我的 ionic 服务器在 localhost:8100 (ionic version -4) & Jira 服务器在 localhost:8089 上运行(JIRA 版本 - 核心 7)

到目前为止我做了什么: 遵循离子博客中提到的代理方法--没有成功

在 JIRA 服务器和列入白名单的 ionic 服务器中启用了 CORS 过滤器插件 -- 不成功

添加了允许控制来源的标题 -- 没有成功

使用--prod release 签名构建apk文件并在设备上尝试相同--没有成功

这是我的 auth.ts 文件参考,如果有人可以在这里提供帮助和建议我在这里做错了什么。

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import { AuthProvider } from '../auth/auth';

import { Issue } from '../../models/issue'

@Injectable()
export class JiraProvider {

  apiVersion: string = '2'; // The API version we want to use
  jiraInstanceUrl: string = 'http://localhost:8089' // The Jira instance URL
  urlString: string = `${this.jiraInstanceUrl}/rest/api/${this.apiVersion}`; // Concat those together

  constructor(
    public http: HttpClient,
    public auth: AuthProvider
  ) {
  }

  // Authenticate the user against Jira's profile endpoint.
  public authenticateUser(username: string, password: string): Observable<Object> {

    return this.http.get(`${this.urlString}/myself`, {
      headers: new HttpHeaders()
        .append('Authorization', `Basic ${btoa(username + ':' + password)}`)
        .append("Access-Control-Allow-Origin","*")
    });
  }

  // Get issue details based on the provided key.
  public getIssue(key: string): Observable<Issue> {
    return this.http.get<Issue>(`${this.urlString}/issue/${key}`, {
      headers: new HttpHeaders()
        .append('Authorization', `Basic ${this.auth.getAuthString()}`)
        .append("Access-Control-Allow-Origin","*")
    });
  }

  public getAllIssue():Observable<any> {
    return this.http.get(`${this.urlString}/search?jql=project=PM`,{
      headers: new HttpHeaders()
        .append('Authorization', `Basic ${this.auth.getAuthString()}`)
        .append("Access-Control-Allow-Origin","*")
    });
  }

  public postIssue(data):Observable<any>{
    return this.http.post(`${this.urlString}/issue`,JSON.stringify(data),{
      headers: new HttpHeaders()
        .append('Authorization', `Basic ${this.auth.getAuthString()}`)
        .append('Content-Type','application/json')
        .append("X-Atlassian-Token", "no-check")
        .append("User-Agent", "xx")
        .append("Access-Control-Allow-Origin","*")
    });
  }
}

根据请求添加屏幕截图(请不要我故意将 JIRA 服务器更改为 8089 相应地更新了问题)

【问题讨论】:

  • 您能从开发者工具控制台添加屏幕截图吗?
  • @Sescudero 添加截图

标签: ionic-framework cors ionic3 jira jira-rest-api


【解决方案1】:

当您需要访问控制允许来源时,我通过在容器 (Docker) 中使用 CORS 代理解决了这个问题:*`!这就像一个代理https://github.com/imjacobclark/cors-container,现在我可以路由请求了。 希望对其他人有所帮助!

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 2019-03-04
    • 2017-03-05
    • 2017-01-23
    • 2016-09-17
    • 2016-03-23
    • 2017-04-27
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多