Hello!
Problem processing an AJAX request to the server

"SyntaxError: Unexpected token <in JSON at position 0"

AJAX code:

$.ajax({ url: this.props.url, dataType: 'json', success: function(data) { var json = JSON.parse(data) this.setState({data: json}); }.bind(this), error: function(xhr, status, err, data) { console.error(this.props.url, status, err.toString()); }.bind(this) }); }, 

Server code:

 var http = require('http'); var fs = require("fs"); http.createServer(function (request, response) { var req = request.url; fs.readFile(req, function (err, data) { response.writeHead(200, { 'Content-Type': 'application/json', "Access-Control-Allow-Origin": "*" }); response.write(data); response.end(); }); }).listen(3000); 
  • one
    It seems that the server returns HTML. Give the content of the server response. - Sergiks
  • one
    What's in the data ? Explicitly HTML , and the Content-Type header says that it is JSON , so the browser tries to parse the content as JSON , and there is no < outside lines. - user207618
  • Dropped the server's response, it returns the contents of index.html prntscr.com/bqizqa - Trufinol
  • one
    Put the server response ( data ) in your question. More precisely, the resolved question - the content is not a valid JSON format, it can not be given with application/json . - user207618 pm

0