Ajax PUT with jquery to MongoDB

820 views Asked by At

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"
            });
1

There are 1 answers

0
JohnnyHK On BEST ANSWER

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"
});