繁体   English   中英

从 API 获取数据的 POST 请求

[英]POST-request to get data from API

我正在尝试为我的 API 创建一个登录系统,但我似乎无法在我的 Javascript 代码中得到回复。 但是,在 POSTman 中,我可以:

https://i.stack.imgur.com/WneNm.png

这是我的 javascript 代码:

 function loginUser(email, password) { let person = {Email: email, Password: password}; fetch(UrlAuthenticationToken, { method: "POST", body: JSON.stringify(person), headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }).then((response) =>{ if (response.status === 200) { console.log(`Logged in ${response.status}`); return response.json(); // only for generating token } else { throw new Error(`error with status ${response.status}`); } }).then((response) => { let accessPass = { Token: reponse.Token, User: { Email: reponse.User.Email, Type: reponse.User.Type } } sessionStorage.setItem(accessPass); if(response.User.Type === 'Student'){ window.href(UrlStudent); } else if(response.User.Type === 'Lector'){ window.href(UrlLecturer) } else if(response.User.Type === 'Business'){ window.href(UrlCompany) } }).catch((e) => { Console.log(e); }); };

问题是,我可以将我的 JSON 正文发送到后端,后端确实返回响应,但前端似乎无法处理所述响应。 我想知道出了什么问题; 当我调试我的代码从 first.then 一直到最后,跳过 rest。

像这样?

$.ajax({
  type : "POST",
  url : "http://localhost/service.asp",
  data: {Email: "asd@asd.asd", Password: "asdasd"},
  //data: '{Email: "asd@asd.asd", Password: "asdasd"}',

  headers : {          
    "Accept" : "application/json; charset=utf-8",         
    "Content-Type": "application/json; charset=utf-8"   
  }        
  success : function(response) {  
    Console.log(response);
  }
});

我看到的是您使用的是大写键,但在您的 postman 中,我可以看到所有键都是小写的。 您使用了带有大写 T 的response.Token之类的键。但是您从 postman 的响应似乎就像带有小 t 的response.token 除此之外,您的代码也有一些类型错误。 您在 accessPass 变量中的响应拼写错误。

您的 function 需要像:-

function loginUser(email, password) {
    let person = {Email: email, Password: password};
    fetch(UrlAuthenticationToken, {
        method: "POST",
        body: JSON.stringify(person),
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
    })
        .then((response) =>{
            if (response.status === 200) {
                console.log(`Logged in ${response.status}`);
                return response.json(); // only for generating token
            } else {
                throw new Error(`error with status ${response.status}`);
            }
        })
        .then((response) => {

            let accessPass =
            {
                Token: response.token,
                User:
                {
                    Email: response.user.email,
                    Type: response.user.type
                }
            }

            sessionStorage.setItem(accessPass);

            if(response.user.type === 'Student'){
                window.href(UrlStudent);
            }
            else if(response.user.type === 'Lector'){
                window.href(UrlLecturer)
            }
            else if(response.user.type === 'Business'){
                window.href(UrlCompany)
            }
        })
        .catch((e) => {
            Console.log(e);
        });
};

暂无
暂无

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

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