最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get http status code of request in Vue and Ajax - Stack Overflow

programmeradmin3浏览0评论

I would like to get the HTTP status code when a form has been sent(the function for sending the form...):

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)):

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

I would like to get the HTTP status code when a form has been sent(the function for sending the form...):

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)):

if (statusCode = 201) {
 *the code which show the notification for success* } 
else { *the code which show the notification for correct errors* }
Share Improve this question edited Jan 7, 2020 at 8:06 xx5ko asked Oct 11, 2018 at 7:34 xx5koxx5ko 2152 gold badges4 silver badges15 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

Using the then() function you can handle the response of your call. 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);
});

发布评论

评论列表(0)

  1. 暂无评论