-1

I've created and testing my API which responds with authorized or not-authorized when the credentials have been typed in. I have tested this with postman and works perfectly.

The login I am trying to create is on React and is currently looking like this -

import React, { Component } from "react";
import { Button, Form, FormGroup, Input } from "reactstrap";
import { withRouter } from "react-router-dom";
import auth from "./auth";

export class Login extends React.Component {
    constructor(props) {
        super(props);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    login = () => {
        auth.login();
    };

    handleSubmit(event) {
        event.preventDefault();
        fetch('http://localhost:5000/api/LoginTest', {
            method: 'post',
            headers: { 'Content-Type': 'application/json' },
            body: {
                "username": this.username.value,
                "password": this.username.value
            }
        });
    };

    render() {
        return (
            <div className="Login">
                <form onSubmit={this.handleSubmit}> 
                        <h2 id="formTitle" className="text-center">
                        Admin Panel
          </h2> 
                        <input
                            id="inputUsername"
                            type="text"
                            placeholder="Username"
                            name="username"
                            ref={(ref) => { this.username = ref }}
                        />

                        <input
                            id="inputPassword"
                            type="password"
                            placeholder="Password"
                            name="password"
                            ref={(ref) => { this.password = ref }}
                        />

                    <button
id="button"
                        type="submit"
                        className="btn btn-block btn-success"
                    >
                        Sign-in
          </button>
                </form>
            </div>
        );
    }
}
export default withRouter(Login);

The 2 errors which I'm currently getting are -

  • 415 Unsupported Media Type OR When I use mode: no-cors
  • Acces to fetch at "" from origin has been blocked by the CORS policy

Any help on this would be perfect!

Hughesey
  • 358
  • 1
  • 17
  • 1
    This is a [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) and has to be fixed at the backend or run through a [proxy](https://create-react-app.dev/docs/proxying-api-requests-in-development/). Check [docs](https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api) – Agney Feb 19 '20 at 14:45
  • did you try checking other post by searching before posting your question? Cors are a really common problem and you should find something [here](https://stackoverflow.com/search?q=Cors+react). Some things like [this](https://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work) or [this](https://stackoverflow.com/a/41497848/8300513) – Palencar Feb 19 '20 at 15:08
  • I have had a good look around regarding CORS but a lot of solution haven't been working. I'm just looking through these Microsoft docs now cheers – Hughesey Feb 19 '20 at 15:11

1 Answers1

0

In your fetch options, try setting: mode: 'cors'

Also make sure that you stringify() your body data

Take a look at this example on the MDN site

Jackson
  • 3,476
  • 1
  • 19
  • 29