Estoy tratando de acceder a datos json desde un servidor local
En mi cliente.js
var xhttp window.onload = function(){ requestData("http://localhost:8000/js/item_data.json"); } function requestData(URL){ xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = processData; xhttp.open("GET", URL, true); console.log(xhttp); //it prints information with status: 0 } function processData(){ console.log(xhttp.status); //prints 0 if(xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) { // do something } else{ console.log("ERROR"); } //This is always printed }En mi servidor.js
const http = require("http"); const file = require("fs"); const host = "localhost"; const port = 8000; const server = http.createServer(processRequest); function processRequest(request, response){ if(request.url === "/index.html"){ file.readFile('../index.html', 'utf8', function(err, contents) { if(err){ response.writeHead(500, { "Content-Type": "text/html"}); response.end(); return; } response.writeHead(200, { "Content-Type": "text/html"}); response.end(contents); }); else if(request.url === "/js/item_data.json"){ file.readFile('item_data.json', 'utf8', function(err, contents) { if(err){ response.writeHead(500, { "Content-Type": "application/javascript"}); response.end(); return; } response.writeHead(200, { "Content-Type": "application/javascript"}); response.end(contents); }); } } He intentado cambiar el tipo de contenido a application/json
Intenté usar /js/item_data.json
Y solicitud.url === "/item_data.json"
los dos no trabajan