Home > Enterprise >  How to use dynamic input to string?
How to use dynamic input to string?

Time:12-27

I'm trying to send GET request with params to backend

const email = document.querySelector('.formEmail');
const pass = document.querySelector(".formPass");
const requestURL = 'http://localhost:8080/users/login?email=${email.value}&password=${pass.value}'
  const sendRequest = (method, url) => {
    return new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest()
      xhr.open(method, url);
      xhr.responseType = 'json'
      xhr.onload = () => {
        if (xhr.status >= 400 && xhr.status < 500) {
          reject(xhr.response)
          return error = true
        } else if (xhr.status >= 500 && xhr.status < 600) {
          reject(xhr.response)
        }
        else {
          resolve(xhr.response)
        }
      }
      xhr.onerror = () => {
        resolve(xhr.response)
      }
      xhr.send();
    })
  }
sendRequest("GET", requestURL)
          .then((data) => {
              alert('user email n pass found')
              window.location.href = '#routeToHomePage'; 
          })

but i don't know what i'm doing wrong because it seems to me that I am passing the input values correctly this code is a part of function on submit button

also i'm getting follow mistake

Access to XMLHttpRequest at 'http://localhost:8080/users/login?email=${email.value}&password=${pass.value}' from origin 'http://localhost:63342' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
auth-script.js:39          GET http://localhost:8080/users/login?email=${email.value}&password=${pass.value} net::ERR_FAILED 400
(anonymous) @ auth-script.js:39
sendRequest @ auth-script.js:21
submit @ auth-script.js:51
(anonymous) @ auth-script.js:131
auth-script.js:52 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'email')
    at auth-script.js:52:40
(anonymous) @ auth-script.js:52
Promise.then (async)
submit @ auth-script.js:53
(anonymous) @ auth-script.js:131
auth-index.html:1 Access to XMLHttpRequest at 'http://localhost:8080/users/login?email=${email.value}&password=${pass.value}' from origin 'http://localhost:63342' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
auth-script.js:39          GET http://localhost:8080/users/login?email=${email.value}&password=${pass.value} net::ERR_FAILED 400
(anonymous) @ auth-script.js:39
sendRequest @ auth-script.js:21
submit @ auth-script.js:51
(anonymous) @ auth-script.js:131
auth-script.js:52 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'email')
    at auth-script.js:52:40

CodePudding user response:

It looks like you are trying to send an HTTP GET request to the backend with email and password as query parameters. Here are some things you can consider:

  1. Make sure that the backend server is running and the endpoint is correct. You can test this by visiting the endpoint in a web browser or using a tool like cURL.

  2. Make sure that you are correctly extracting the values of the email and pass elements and formatting them as query parameters in the request URL. Currently, you are using template literals (${}) instead of string concatenation to add the values to the URL. You should replace 'http://localhost:8080/users/login?email=${email.value}&password=${pass.value}' with 'http://localhost:8080/users/login?email=' email.value '&password=' pass.value to correctly build the URL.

  3. You might want to consider handling errors in the response more explicitly. Currently, you are only rejecting the promise if the status code is in a certain range. You may want to add specific handling for different status codes (e.g., 404 for not found, 401 for unauthorized) or add more specific error messages to help debug any issues.

CodePudding user response:

To use dynamic input as a string in Python, you can use the input() function. This function reads a line of text from the user and returns it as a string. Here's an example of how to use it:

  • Related