0

我正在创建一个使用 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);
    });
}
}
4

1 回答 1

0

事实证明,在这种情况下,您不能使用 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);
});
}
}
于 2015-05-25T17:57:56.687 回答