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!
users.jsonas well please.