3

I'm working on a small web framework to run a HCI study in and came across the following problem:

I have a Node server running with express to serve my local host data from JSON files. Not the best db but since it's a single user system (only one participant will ever be using the system at any time) it really didn't make sense to add any other technology. The following Get request code works just fine:

function getUser(id,last) {
return $.ajax({
    type: "GET",
	url: "/data/user/"+id,
	async: false
	}).responseJSON;
}

Which is handled by the following node code:

app.get('/data/:asset/:id', function (req, res) {
var accJSN
res.setHeader('Content-Type', 'application/json');

if(req.params.asset === "user")
{
    accJSN = JSON.parse(fs.readFileSync(path.join(__dirname,'/public/data/users.json')));

    res.send(JSON.stringify(accJSN.users[req.params.id]));
}

The above code produces a response which I can use/print and contains the responseJSON attribute. The following code does not, I'll note the node code is in the same server.js file and the functions with jquery/ajax calls are in my client page:

Client side code:

function getUserset(ids,last) {
    userQuery = "";

    for(i=0;i<ids.length;i++)
    {
        userQuery += ids[i] + ",";
    }
	userQuery = userQuery.slice(0,-1);

	return $.ajax({
	    type: "GET",
	    url: "/userset?users=["+userQuery+"]",
	    async: false
        }).responseJSON;
}

Server code:

app.get('/userset', function (req, res) {

    var accJSN = JSON.parse(fs.readFileSync(path.join(__dirname,'/public/data/users.json')));
    accJSN = accJSN.users;

    var users = JSON.parse(req.query.users);

    var resJSN = new Array;

    for(var i=0;i<users.length;i++)
    {
        var temp = {};
        temp["id"] = users[i];
        temp["fName"] = accJSN[users[i]].fName;
        temp["lName"] = accJSN[users[i]].lName;
        temp["profilePic"] = accJSN[users[i]].profilePic; 
        resJSN.push(temp);
    }
    res.setHeader('Content-Type', 'application/json'); 
    res.send(JSON.stringify(resJSN));
})

The data I need is actually available in the response text but I cannot for the life of me figure out why the second example doesn't also include the responseJSON attribute, or, if I'm totally wrong, why the first one does. Any thoughts or solutions appreciated, thanks!

1
  • could you also post the actual response received and the an object from users.json as well please. Commented Jan 25, 2016 at 21:32

1 Answer 1

2

Instead of manually setting the application/json header and sending a response just use

res.json(resJSN);

instead of

res.setHeader('Content-Type', 'application/json'); 
res.send(JSON.stringify(resJSN));
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I did moments before noticing this comment. Still, I don't understand why one sent with the attribute and one sent without.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.