chackerian chackerian - 4 months ago 7
Ajax Question

Local Ajax call provides no response text

I'm trying to send an ajax call from the client to my server with data regarding an inputted paramater. When I attempt this, I am able to only see the data in my servers console, not in the browser.

Here in the client is the ajax call with "Peter_Abelard" setup as a demo title. This call gives me a 200 ok status but the response text is empty.

$.ajax({
type: "GET",
url: 'http://localhost:3000/api/wiki/Peter_Abelard',
async: false,
datatype: 'json',
success: function(response){
console.log(response);
}
});


In the server code I have

function getData(name){
wikipedia.page.data(name, { content: true }, function(response) {
console.log(JSON.stringify(response));

var dad = JSON.stringify(response);

fs.writeFile("wikip.txt", dad, function(err) {
if (err) throw err;
console.log('It\'s saved!');
});

return dad;
});
}

app.get('/api/wiki/:que', function(req, res) {
var queryd = req.params.que;
getData(queryd);
res.send(getData(queryd));
});


I believe this problem has something to with the line
res.send(getData(queryd))
but I'm not sure what to try. If I am correct, this line should send the proper text to the client.

Answer

There are several things wrong with your current approach:

  1. There is no return value from the actual getData() function. That's why you always get undefined. You are returning from an inner callback, but that doesn't do anything.

  2. Your result is asynchronous so there's no way it can be returned from getData() because the result is not even known yet when getData() returns. An asynchronous response means that your Javascript continues to run (thus getData() returns empty handed) and the async callback is called sometime later.

  3. To make this work, getData() needs to either accept a callback which it can call when the data is available or it needs to return a promise and then the caller needs to use that callback or promise appropriately to get the data.

To make this work, you have to know how to handle asynchronous operation in node.js. I'd suggest this:

function getData(name, cb) {
    wikipedia.page.data(name, {content: true}, function (response) {
        console.log(response);

        cb(null, response);

        // I'm not sure why you're writing this here
        fs.writeFile("wikip.txt", dad, function (err) {
            if (err) throw err;
            console.log('It\'s saved!');
        });
    });
}

app.get('/api/wiki/:que', function (req, res) {
    var queryd = req.params.que;
    getData(queryd, function (err, data) {
        if (err) {
            // send error response here
        } else {
            res.json(data);
        }
    });
});

P.S. It looks very odd to me that wikipedia.page.data() has an async callback response, but no error reporting mechanism.

You can learn more about dealing with async operations in node.js by reading this answer: How do I return a response from an asynchronous operation.

Comments