简体   繁体   English

从 API 获取数据的 POST 请求

[英]POST-request to get data from API

I'm trying to make a login system for my API, but I can't seem to get a response back in my Javascript code.我正在尝试为我的 API 创建一个登录系统,但我似乎无法在我的 Javascript 代码中得到回复。 However, in POSTman, I can:但是,在 POSTman 中,我可以:

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

This is my javascript code:这是我的 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); }); };

The thing is, I can send my JSON-body to the back-end and the back-end does return a response, but the front end can't seem to handle said response.问题是,我可以将我的 JSON 正文发送到后端,后端确实返回响应,但前端似乎无法处理所述响应。 I wonder what is going wrong;我想知道出了什么问题; when I debug my code goes from the first.then all the way to the end, skipping the rest.当我调试我的代码从 first.then 一直到最后,跳过 rest。

Like this?像这样?

$.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);
  }
});

What I see is you are using capitalize keys but in your postman I can see all the keys are in lowercase.我看到的是您使用的是大写键,但在您的 postman 中,我可以看到所有键都是小写的。 You have used key like response.Token with capital T. But your response from postman seems to be like response.token with small t.您使用了带有大写 T 的response.Token之类的键。但是您从 postman 的响应似乎就像带有小 t 的response.token Apart from this your code has some type mistake as well.除此之外,您的代码也有一些类型错误。 You have misspelled response in accessPass variable.您在 accessPass 变量中的响应拼写错误。

Your function needs to be like:-您的 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