-1

When I make a gateway request to the URL manually in my project, it gives me the correct data, but when I try to send an http request with the frontend, the API does not work. Why is that?

JavaScript

<script>
  document.getElementById("signupbtn").addEventListener("click",function(){
    const username = document.getElementById("username").value;
    const password = document.getElementById("password").value;

 
    const xhr = new XMLHttpRequest();
    xhr.open("POST","http://localhost:9000/user/register?username=${username}&password=${password}");
    xhr.send();
  })

</script>

Giving POST http://localhost:9000/user/register?username=${username}&password=${password} 400 (Bad Request)

  • 1
    Please share the error/problem you encounter, 'does not work' doesn't help us to help you. – idanz Jul 28 '23 at 13:20
  • The problem is I ve not encounter any error, it is just not working. I didnt find a answer for this like 7 hour. My question is how can i send http request to api gateway from frontend using button – MatildaEliz Jul 28 '23 at 13:23
  • Nope, not good enough. "Just not working" simply doesn't give us enough information to help you. You're going to have to figure out *what* isn't working: Is the request being sent from the browser? Arriving at the server? Are you getting a response? Et cetera. – Jorn Jul 28 '23 at 13:26
  • When I run this url manually http://localhost:9000/user/register?username&password it is working but when I try to send http request via frontend button nothing occur – MatildaEliz Jul 28 '23 at 13:27
  • 'Nothing occurs' is not possible, something for sure occurs. You run the command `xht.Send()`, debug your code and share with us what happens when the command being executed. – idanz Jul 28 '23 at 13:30
  • how can i debug it I dont know how in JavaScript – MatildaEliz Jul 28 '23 at 13:33
  • https://developer.chrome.com/docs/devtools/javascript/ Once you debug and understand the problem, please edit your post with the details. – idanz Jul 28 '23 at 13:35
  • added with image – MatildaEliz Jul 28 '23 at 14:16
  • https://stackoverflow.com/questions/35553500/xmlhttprequest-cannot-load-xxx-no-access-control-allow-origin-header – idanz Jul 28 '23 at 15:24
  • occurs now . signup.html:53 POST http://localhost:9000/user/register?username=${username}&password=${password} 400 (Bad Request) – MatildaEliz Jul 28 '23 at 15:58
  • Check if the way you tried to inject the username and password to the url actually works... – idanz Jul 28 '23 at 16:28

2 Answers2

0

The syntax of your request is incorrect if you want to replace the variables in your template string. You should be using the backtick ` instead of the quote ".

Instead of

"http://localhost:9000/user/register?username=${username}&password=${password}"

You should be using

`http://localhost:9000/user/register?username=${username}&password=${password}`
guest
  • 46
  • 1
-2

I am not sure if you would be able to send a API request to a gateway from your frontend without any communicator.

Arjun
  • 1