Search code examples
jqueryajaxmongodbputmlab

Ajax PUT with jquery to MongoDB


I want to update the position of a user in my MongoDB. Mongolab docs say I should use Ajax with PUT, but it doesn't work.

http://docs.mongolab.com/data-api/

return $.ajax({
                url: "https://api.mongolab.com/api/1/databases/trampdaten/collections/points?apiKey=KEYKEYKEY&q={\"User\": "+username+"}&u=true",
                type: "PUT",
                data: JSON.stringify({User: username, Latitude: crd.latitude, Longitude: crd.longitude, Time: datetime()}),
                contentType: "application/json"
            });

Solution

  • I can't confirm it's your only problem, but your username value in the url needs to be surrounded by " chars to make it a valid JSON string:

    return $.ajax({
        url: "https://api.mongolab.com/api/1/databases/trampdaten/collections/points?apiKey=KEYKEYKEY&q={\"User\": \""+username+"\"}&u=true",
        type: "PUT",
        data: JSON.stringify({User: username, Latitude: crd.latitude, Longitude: crd.longitude, Time: datetime()}),
        contentType: "application/json"
    });