0

Everything is working fine with the forms post requests I've done so far, but when I try to make an Ajax call I'm not able to deal with it on the server. Although it returns the 'succeed' param, I'm not able to save the information on the database nor make any acions with it on the server.

page.jade:

script
        (function () {

            $('ul').find('a').on("click", function () {
                var option = $(this).data('res');
                $('article').animate({ 'margin-left': -600 });
                console.log(option + ' ' + #{idn});

                var envio = {resultado: option, idupdate: #{idn}};

                $.ajax({
                        url: '/',
                        type: "post",
                        data: JSON.stringify(envio),
                        contentType: 'application/json',
                        success: function(data) {
                            console.log('success');
                        }
                    });
            });
        })();

server.js

app.post('/', function (req, res) {

    var Resultado = req.body.resultado;
    var idUpdate = req.body.idupdate;

    if (Resultado == "yes") {
        connection.query('UPDATE questions SET yes=yes+1 WHERE id=' + idUpdate + ');');
    } else {
        connection.query('UPDATE questions SET no=no+1 WHERE id=' + idUpdate + ');');
    }
});

I tried to find any similar situation around, but the information on this particular case is scarce. Thanks!!!

1 Answer 1

1

Looks like both querys are malformed,

connection.query('UPDATE questions SET yes=yes+1 WHERE id=' + idUpdate + ');');
                                                           (here)   ----^   
connection.query('UPDATE questions SET no=no+1 WHERE id=' + idUpdate + ');');
                                                     (and here)   ----^ 

Then, remove + ');' part and it should work.

Like:

connection.query('UPDATE questions SET yes=yes+1 WHERE id=' + idUpdate);

Aditionaly you need to end the request, calling res.end()

final code:

app.post('/', function (req, res) {

    var Resultado = req.body.resultado;
    var idUpdate = req.body.idupdate;

    if (Resultado == "yes") {
        connection.query('UPDATE questions SET yes=yes+1 WHERE id=' + idUpdate);
    } else {
        connection.query('UPDATE questions SET no=no+1 WHERE id=' + idUpdate);
    }
    res.end(); // must be called.
});
Sign up to request clarification or add additional context in comments.

3 Comments

yep! That was it! Thanks @3boll ! Now the query is writing to the database, but I's not returning the success param back to the console. Any idea?
Note, this query is wide-open to SQL injection. You should be using prepared statements or escaping the ID with connection.escape(idUpdate). Example: Submit an ID of 1 OR 1=1 would update the yes count on every question.
@loganfsmyth , could you tell me a good article I can study about it? Thanks a lot

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.