Sunday, 19 March 2017

jQuery getJSON fails with 404 error

The URL that the getJSON request is sent to definitely exists, but the request fails with a 404 error. The URL just hosts a JSON object: here. I've also tried using the same request but replacing the destination with a local JSON file hosted in the same directory, which also fails with a 404 error. I'm guessing this means that the error is either with my getJSON request, or with my node.js server configuration.

This is the function that makes the getJSON call:

function loginFunction(){
        //check browser support
        if(typeof(Storage) !== "undefined"){
            //store dat shit
            sessionStorage.setItem("username", document.getElementById('username').value);
            sessionStorage.setItem("password", document.getElementById('password').value);
            $(document).ready(function(){
                $.getJSON(createUsernameURL(), function(data){
                    console.log(data);
                    var responseUsername = data.username;
                    document.getElementById("unresult").innerHTML = responseUsername;
                    var responsePassword = data.password;
                    document.getElementById("pwresult").innerHTML = responsePassword;
                });
            });
        }else{
            document.getElementById("pwresult").innerHTML = "your browser is out of date";
        }

And this is the config file for my node.js server:

const http = require('http');
const express = require('express');
const app = express();

app.listen(3000,function(){
    console.log(__dirname)
});

app.get('/', (req,res) => {
    res.sendFile(__dirname + '/index.html');
});

app.use("/static", express.static(__dirname + '/static'));

The createUsernameURL() function just appends a couple pieces of user-entered information to a base URL, but even hard-coding the exact database link mentioned above gives the same issues.



via Clayton Keleher

No comments:

Post a Comment