javascript - 如何在不返回 Angular 数据的 http.post 请求上使用 'toPromise()'?

标签 javascript angular typescript promise request

我最近开始学习 Angular (4) 并遵循了 Angular.io 的教程。

但现在我正在尝试构建自己的应用程序,但遇到了一些问题。我花了一整天的时间试图解决这个问题,但我失败了。

我正在开发一个目前只有登录和注销的身份验证服务。

login 工作正常,但我没有让 logout 工作。它似乎中断了 toPromise() 调用。

const url = `${environment.serviceURL}/api/account/logout`;
const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded',
    'withCredentials':true,
    'Authorization': `Bearer ${this.token}`});
let options = new RequestOptions({headers: headers});
let result = this.http.post(
     url, options).toPromise().then(/*do something*/);

这让我在控制台中出现以下错误:

ERROR Error: Uncaught (in promise): TypeError: v.split is not a function
TypeError: v.split is not a function at http://localhost:4200/vendor.bundle.js:26382:76
at Array.forEach (native)
at http://localhost:4200/vendor.bundle.js:26382:20
at Map.forEach (native)
at Headers.toJSON (http://localhost:4200/vendor.bundle.js:26380:23)
at Object.stringify (<anonymous>)
at Request.Body.text (http://localhost:4200/vendor.bundle.js:26957:25)
at Request.getBody (http://localhost:4200/vendor.bundle.js:27865:29)
at Observable._subscribe (http://localhost:4200/vendor.bundle.js:27393:37)
at Observable._trySubscribe (http://localhost:4200/vendor.bundle.js:567:25)
at http://localhost:4200/vendor.bundle.js:26382:76
at Array.forEach (native)
at http://localhost:4200/vendor.bundle.js:26382:20
at Map.forEach (native)
at Headers.toJSON (http://localhost:4200/vendor.bundle.js:26380:23)
at Object.stringify (<anonymous>)
at Request.Body.text (http://localhost:4200/vendor.bundle.js:26957:25)
at Request.getBody (http://localhost:4200/vendor.bundle.js:27865:29)
at Observable._subscribe (http://localhost:4200/vendor.bundle.js:27393:37)
at Observable._trySubscribe (http://localhost:4200/vendor.bundle.js:567:25)
at resolvePromise (http://localhost:4200/polyfills.bundle.js:3224:31)
at http://localhost:4200/polyfills.bundle.js:3150:17
at SafeSubscriber._error (http://localhost:4200/vendor.bundle.js:66387:85)
at SafeSubscriber.__tryOrSetError (http://localhost:4200/vendor.bundle.js:16367:16)
at SafeSubscriber.error (http://localhost:4200/vendor.bundle.js:16321:26)
at Subscriber._error (http://localhost:4200/vendor.bundle.js:16248:26)
at Subscriber.error (http://localhost:4200/vendor.bundle.js:16222:18)
at Observable._trySubscribe (http://localhost:4200/vendor.bundle.js:572:18)
at Observable.subscribe (http://localhost:4200/vendor.bundle.js:555:27)
at http://localhost:4200/vendor.bundle.js:66387:15

主要区别在于(至少我认为)login 返回包含用户信息的数据,而logout 不返回任何数据。它只是删除服务中的 session 。

我只想知道请求是否成功,以便我可以采取行动。 我做错了什么或者我应该做些什么来让它工作?

最佳答案

正如@Bunyamin Coskuner 和@BeetleJuice 在评论中指出的那样,这与 toPromise() 无关。 我发现我在调用没有 body 参数的 http.post 方法时犯了一个错误,导致函数将 options 作为主体。

变化:

let result = this.http.post(
 url, options).toPromise().then(/*do something*/);

到:

let result = this.http.post(
 url, null, options).toPromise().then(/*do something*/);

解决了我的错误!

关于javascript - 如何在不返回 Angular 数据的 http.post 请求上使用 'toPromise()'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44740209/

相关文章:

javascript - 客户端和服务器端编程有什么区别?

javascript - chrome.runtime.OnMessage.addListener 返回重复消息

angular - 将 Angular 5 更新为 Angular 6 错误

javascript - 将div分成2列

使用 NgbDatepicker 的 Angular 单元测试失败

javascript - 如何使用 JavaScript 禁用 html 表单元素?

javascript - bluebird Promise - 如何从回调中返回 Promise?

javascript - 有效载荷数据类型是什么?

javascript - Typescript BeforeUnload 事件未触发

typescript - typescript 中 gcloud param DefineSecret 的类型 'value' 不存在属性 'SecretParam'