A
A
AntonyPy2020-12-03 15:40:36
Flask
AntonyPy, 2020-12-03 15:40:36

How to fix JSON.parse error?

Good afternoon, I encountered such a problem:
When sending login / password data to the server, I get an error:

JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

Method of sending data to the server:
let response = await fetch(URL_SERVER_LINK + "/login_client", {
        method: "POST",
        mode: "no-cors",
        credentials: "include",
        headers: {
          "Access-Control-Allow-Origin": "*",
          Accept: "application/json",
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          Username: login,
          Password: password,
        }),
      });

The server receives data like this:

@application.route('/login_client', methods=['POST'])
def login_client():
  auth = request.authorization
  username = request.json.get('username', None)
  password = request.json.get('password', None)
  if not auth or not auth.username or not auth.password:
    return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})

  client = Client.query.filter_by(name=auth.username).first()
  if not client:
    return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
  if check_password_hash(client.password, auth.password):
    return jsonify({"token": create_access_token(identity=auth.username)})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2020-12-03
@phaggi

This thing in the request looks, in my opinion, wrong:

{
          Username: login,
          Password: password,
        }

I think it should be something like:
{
          'Username': login,
          'Password': password,
        }

But maybe there's something I don't understand...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question