I have an assignment about using JavaScript API. I don’t know anything about JavaScript APIs. Can anyone help me? I wrote the login page with html CSS code. But the second stage of the assignment:
- When the login button on the login page is clicked, the values entered in the username and password inputs will be received with JavaScript and a request will be sent to the service here https://fakestoreapi.com/docs#a-login. When a response comes from this service, a redirection will be made to the dashboard page from the login page.
I’m very new to the software, I searched for a video to learn, but I couldn’t find it for the login page. Can anyone help me.
I was able to create a login page using only html CSS.
2
Answers
This is how I would implement it
I created a form and a submit event for it. I preventDefault in the submit event so we don’t do a postback. We also specify that our values are JSON and convert our collected values into JSON.
Please check out this before anything else:
You said in your question that you code this using html and css, so there should be a
<form>
attribute and because you don’t send a back-end server, so I guess this will do:The code provided here https://fakestoreapi.com/docs#a-login do not have
header type
method, remember to add it in. Once received the data you should change how the code should react in each of their corresponding section.In the success case, it returns JSON format:
The failed case returns an error (and seemingly an HTML page), so I use
.catch
to catch it:Hope this helps!