【问题标题】:Aurelia web application calling azure mobile services API endpointAurelia Web 应用程序调用 azure 移动服务 API 端点
【发布时间】:2015-05-26 01:57:56
【问题描述】:

我正在创建一个使用 API 的 Aurelia Web 项目。 API 以 Azure 移动服务的形式提供。我知道我需要将 X-ZUMO Auth 标头添加到请求中。但是,当我实例化我的 http 客户端时,该标头永远不会根据浏览器开发工具进入请求标头。当我在我的应用程序中运行它时,我会看到一个登录屏幕,我认为是因为 X-ZUMO 标头不是存在,因此该应用程序没有运行权限。我正在使用 gulp 运行它,它正在为我设置 Web 应用程序的本地实例。我也试过外部IP地址。

这是我的课:

import {HttpClient} from 'aurelia-http-client';

export class WebApi
{
static inject = [HttpClient];
constructor(http)
{
    this.http = http;
    this.baseUrl = 'https://myProject.azure-mobile.net/';
    this.zumoHeader = 'X-ZUMO-APPLICATION';
    this.zumoKey = 'zumoKey';
    this.client = new HttpClient()
        .configure(x => {
        x.withBaseUrl(this.baseUrl);
        x.withHeader(this.zumoHeader, this.zumoKey);
    });
}

// requests will go here
testGet()
{
    this.client.jsonp('api/logs?application_id=sessionID&__count=20')
    .then(response =>
    {
        alert(response);
    });
}
}

【问题讨论】:

    标签: azure azure-mobile-services aurelia


    【解决方案1】:

    事实证明,在这种情况下你不能使用 jsonp 方法。 get 方法(如果您正在发出 get 请求)是添加标头的唯一方法。

    在此之后,我必须确保服务器也可以处理 CORS。

    import {HttpClient} from 'aurelia-http-client';
    
    export class WebApi
    {
    static inject = [HttpClient];
    constructor(http)
    {
    this.http = http;
    this.baseUrl = 'https://myProject.azure-mobile.net/';
    this.zumoHeader = 'X-ZUMO-APPLICATION';
    this.zumoKey = 'zumoKey';
    this.client = new HttpClient()
        .configure(x => {
        x.withBaseUrl(this.baseUrl);
        x.withHeader(this.zumoHeader, this.zumoKey);
    });
    }
    
    // requests will go here
    testGet()
    {
    this.client.get('api/logs?application_id=sessionID&__count=20')
    .then(response =>
    {
        alert(response);
    });
    }
    }
    

    【讨论】:

    • 我认为没有必要将 HttpClient 注入到类中,假设这个例子是完整的。
    猜你喜欢
    • 2022-11-26
    • 1970-01-01
    • 2012-09-04
    • 2012-02-27
    • 1970-01-01
    • 2016-06-28
    • 2023-04-04
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多