简体   繁体   中英

Saving backbone model on parse.com

i can save a model on parse.com writing:

var persona=new Person({username:"filiberto",password:"filiberto"});

persona.save( {
success: function (persona) {
console.log("modello salvato nel db");
console.log(persona.url());

}
});

but if i first fetch a collection,get a specified model and update by save method doesn't work and error is:PUT https://api.parse.com/1/classes/_User/V742NGMTjA 400 (Bad Request)

Models.utenti = new Usercollection();
Models.utenti.fetch({async:false});

var current_user=Models.utenti.get(Parse.User.current().id);  
current_user.set_last_activity();
current_user.save();<----PUT https://api.parse.com/1/classes/_User/V742NGMTjA 400 (Bad   
Request) 

I've tried to update another class on parse.com and works. The class that i can't update is users,maybe a security reason?

I'm unfamiliar with Parse, but according to their API ( https://www.parse.com/docs/rest#users ), a users request should be a POST not a PUT. Backbone uses PUT to update/sync models, whereas POST is used to create new models. The issue might be that Backbone is assuming that your model has already been saved and using the PUT method to update it.

Parse has a nice tutorial demonstrating the users API with Backbone ( https://parse.com/tutorials/todo-app-with-javascript ). You might also find this discussion addressing saving vs updating Backbone models helpful ( Backbone model.save() is sending PUT instead of POST ).

UPDATE: It appears that Parse requires Cross-Origin Resource Sharing (CORS) to modify existing users' data. In practice, this means adding additional information in the request header. See Parse's blog post in the comments below for more info and implementation details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM