Welcome to Headwind MDM Q&A, where you can ask questions and receive answers from other members of the community.

Please do not post bug reports, missing feature requests, or demo inquiries. If you have such an inquiry, submit a contact form.

0 votes

Access to XMLHttpRequest at 'http://hmdm-server:8080/rest/public/jwt/login' from origin 'http://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.


i am getting the above error for this given react code
 

import React from 'react'

import { useState } from 'react'

import md5 from 'md5';

import axios from 'axios';  

const Login = () => {

    const [username, setUsername] = useState('');

    const [password, setPassword] = useState('');

    const handlelogin = async (e) => {

        e.preventDefault();

        console.log(username, password);

        const hashedpassword = md5(password).toUpperCase();

        try {

        const res = await axios.post('http://hmdm-server:8080/rest/public/jwt/login', {

            login: username,

            password: hashedpassword

        });

       

        const token = res.data.token;

        console.log(token);

        localStorage.setItem('token', token);

        alert('Login successful');

        } catch (error) {

        console.error(error);

        }

    }

   

   

  return (

    <>

    <div>Login</div>

    <form onSubmit={handlelogin}>

        <input type="text" placeholder='username' onChange={(e) => setUsername(e.target.value)} />

        <input type="password" placeholder='password' onChange={(e) => setPassword(e.target.value)} />

        <button type='submit'>Login</button>

    </form>

    </>

  )

}

export default Login;

ago by (170 points)

1 Answer

0 votes

Headwind MDM adds the header Access-Control-Allow-Origin: * to each response. It is controlled by common/src/main/java/com/hmdm/filter/ApiOriginFilter.java and server/src/main/java/com/hmdm/guice/module/MainRestModule.java.

If it is disappeared, make sure you didn't alter the Headwind MDM web panel code, do not ignore this header in your React app, and do not have any proxy servers cutting out this header.

ago by (42.3k points)
...