Unexpected token '<', "<!DOCTYPE "... is not valid JSON. Utilizing express and parsing error. Any suggestions would be great
23:23 09 Nov 2022

For the past few days, I have been attempting to solve this error. I am extremely baffled, as I have no idea why this isn't working. Any help or suggestions would be greatly appreciated.

When I attempt to get data from node.js in my client side js, my JSON parser is giving me an error:

uncaught SyntaxError: Unexpected token '<', ")
    at XMLHttpRequest.onLoad (something.js:18:29)

I am using node, js, and html for this, 3 files total. Here is the index.js:

const express = require('express');

const app = express();

app.use(express.static('public'));
app.use(express.json());
app.listen(3000, () => {
  console.log('server started');
});

app.get('/', function(req, res) {
  console.log('hello')
});
app.listen(8080);

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());


app.post('/testing/thisshit', async function(req, res) {
  console.log(req.body);
  var dataToSendToClient = '{"message":"potatohead"}';
  res.send(dataToSendToClient);
});

The html:


  
    
    
    
  
  

hellow world

The client-side JS:

console.log('testing')
var abcd = {juice: "juice"}
  $.post( '/testing/thisshit', abcd);
var res = new XMLHttpRequest();
var url = '/testing/thisshit';
res.open('GET',url,true); // set this to POST if you would like
res.addEventListener('load',onLoad);
res.addEventListener('error',onError);
res.send();
function onLoad() {
  var response = this.responseText;
  console.log(response);
  var parsedResponse = JSON.parse(response);
  console.log("the result: " + parsedResponse)
}
function onError() {
  console.log('error');
}

No matter what I attempt, it seems to baffle me. Any answers or suggestions would be incredibly helpful, and thank you in advance for any suggestions.

javascript node.js json express parsing