dimanche 28 juin 2015

ajax get request in node js express

Hi Guys working a litle bit on my Node js Skills.

Would like to add some data to mongodb using a button click.

Client side code looks like this

        $(function() {
        $('#add').click(function(){
            $.ajax({
                type: "GET",
                url: "/item/<%= products._id %>/add"
            }).done (function (data) {
                alert(data);
                console.log(data);
            });
        });
    });

  <button type="submit" id="add" class="btn btn-primary">Interessted</button>

Server side code like this

    app.get('/item/:id/add', function(req, res) {

    Listing.findByIdAndUpdate(
        { _id : req.params.id},
        { $push : {"product.interessteduser": req.user._id }},
        {  safe: true, upsert: true},
        function(err, model) {
            if(err){
                console.log(err);
            }

        });
});

The Code works perfectly for me. But if i wait a litle bit i get another request in my console.

Looks like this

GET /item/557eec02aa1046b805190207/add 200 120001ms
GET /item/557eeb82aa1046b805190206/add 200 120000ms

So every time request /item/:id/add and wait for 120000ms i get another request. How to stop this?

I would like to hit the button once do the /item/557eeb82aa1046b805190206/add Get request and that's all.

Aucun commentaire:

Enregistrer un commentaire