s96
s96

Reputation: 25

AJAX POST not working in javascript to nodejs

my ajax post doesnt go through. Any idea on how to fix this? it doesnt show anything in the console log. /ajax_check works fine but /report doesnt. I really really need help on this... ive tried console logs to debug but they do not appear.. please help thank you

IN JAVASCRIPT (named popup.js)

    chrome.contextMenus.create({
        title: "Report a Scam Image",
        contexts:["image"],
        onclick: function(info) {
            handleURL(info.srcUrl);
        }
    });

    function handleURL(url) {

            alert(url);
            $.ajax({
                    url: "http://localhost:8023/report",
                    type: "GET",

                    data: {
                        link: url,
                    }

            }).done(function(data) {

            })
            .fail(function() {

                    alert("Error");



            });
    }


    In NodeJS (server.js)

    app.post('/report', function(req, res){
        var getUrl = req.body.link;
        console.log(getUrl);
        var sql = "INSERT INTO reports (link) VALUES ("+getUrl+")";
        console.log(sql);
          con.query(sql, function (err, result) {
            if (err) throw err;
            console.log("1 report entered.");
                res.end("success");
          });



    });

Upvotes: 1

Views: 34

Answers (1)

Bandana Sahu
Bandana Sahu

Reputation: 284

Try this one. Add type as post and datatype as json

           $.ajax({
                    url: "http://localhost:8023/report",
                    type: "POST",
                    dataType: 'json',
                    data: {
                        link: url,
                    }

            }).done(function(data) {

            })
            .fail(function() {
                    alert("Error");
            });       

Upvotes: 1

Related Questions