简体   繁体   English

在Vue和Ajax中获取请求的http状态码

[英]Get http status code of request in Vue and Ajax

I would like to get the HTTP status code when a form has been sent(the function for sending the form...):我想在发送表单时获取 HTTP 状态代码(发送表单的功能...):

return fetch(serviceUrl + 'Collect', {
    method: "POST",
    headers: new Headers({
      "Content-Type": "application/json",
      Authorization: "Bearer " + DataLayer.instance.token
    }),
    body: JSON.stringify(
        (mergedFormObjects),{
        "UserId": this.oidcIdToken 
    }),
  });
}

base on that status code (201 for success; else - "user must correct data) I would like to show notifications(which I am going to/and ready/ use vue-notification framework)):基于该状态代码(成功为 201;否则 - “用户必须更正数据)我想显示通知(我将要/准备好/使用 vue-notification 框架)):

if (statusCode = 201) {
 *the code which show the notification for success* } 
else { *the code which show the notification for correct errors* }

Using the then() function you can handle the response of your call.使用 then() 函数可以处理呼叫的响应。 Accessing the status code is extremely simple.访问状态代码非常简单。 I added a simple snippet which you should be able to adapt for your needs.我添加了一个简单的片段,您应该能够根据自己的需要进行调整。

 return fetch(serviceUrl + 'Collect', { method: "POST", headers: new Headers({ "Content-Type": "application/json", Authorization: "Bearer " + DataLayer.instance.token }), body: JSON.stringify( (mergedFormObjects),{ "UserId": this.oidcIdToken }), }).then(function(response){ console.log(response.status); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM