Javascript Backbone.js save model don';t导致android 2.x上的ajax PUT请求
Phonegap+主干网+jquery mobile。 我的主干模型看起来像(删除了一些代码) 更新采购逻辑重新计算ShoppingList的总价Javascript Backbone.js save model don';t导致android 2.x上的ajax PUT请求,javascript,android,backbone.js,cordova,Javascript,Android,Backbone.js,Cordova,Phonegap+主干网+jquery mobile。 我的主干模型看起来像(删除了一些代码) 更新采购逻辑重新计算ShoppingList的总价 save: function(position){ var data = this.$el.find("form").serializeObject(); var price = parseFloat(data.price); var quantity = parseFlo
save: function(position){
var data = this.$el.find("form").serializeObject();
var price = parseFloat(data.price);
var quantity = parseFloat(data.quantity);
var product = new Purchase(data);
product.on('invalid', this.showError);
var shopping = GlobalShopping.findWhere({id: data.shopping});
if(!isNaN(price) && !isNaN(quantity))
shopping.set('totalPrice', shopping.get('totalPrice') + price * quantity);
if(!isNaN(price))
product.set('price', price);
else
product.unset('price');
if(!isNaN(quantity))
product.set('quantity', quantity);
if(this.model.id){
var oldProduct = shopping.get('products').findWhere({id: this.model.id});
if(oldProduct.get('price') != null)
shopping.set('totalPrice', shopping.get('totalPrice') - oldProduct.get('price') * oldProduct.get('quantity'));
product.id = this.model.id;
product.save({}, { success: function(){
shopping.get('products').add([product], {merge: true});
shopping.save({}, {success: function(){ // <<<< issue here
window.app_router.navigate('#shopping/' + data.shopping, {replace: true, trigger: true});
}});
}});
} else {
shopping.get('products').create(product, {wait: true, success: function(){
shopping.save({}, {success: function(){
window.app_router.navigate('#shopping/' + data.shopping, {replace: true, trigger: true});
}});
}, error: function(model, xhr, options){
alert(JSON.stringify(product.toJSON()));
}});
}
}
保存:功能(位置){
var data=this.$el.find(“form”).serializeObject();
var价格=parseFloat(data.price);
变量数量=解析浮点(data.quantity);
var产品=新购买(数据);
product.on('无效',本.RoR);
var shopping=GlobalShopping.findWhere({id:data.shopping});
如果(!isNaN(价格)和&!isNaN(数量))
shopping.set('totalPrice',shopping.get('totalPrice')+价格*数量);
如果(!isNaN(价格))
产品集(“价格”,价格);
其他的
产品未结算(“价格”);
如果(!isNaN(数量))
产品集合(“数量”,数量);
if(this.model.id){
var oldProduct=shopping.get('products').findWhere({id:this.model.id});
if(oldProduct.get('price')!=null)
shopping.set('totalPrice',shopping.get('totalPrice')-oldProduct.get('price')*oldProduct.get('quantity');
product.id=this.model.id;
product.save({},{success:function(){
shopping.get('products').add([product],{merge:true});
shopping.save({},{success:function(){/如果查看主干源,则仅当尝试保存的模型具有Id时,才会触发PUT请求。是否可以尝试以下操作:
product.set('id', 1);
product.save();
快速浏览一下您的代码,我不确定您是如何填充产品的。您是先获取它,然后尝试重新保存它吗?无论如何,如果未设置Id,主干将发出POST,如果设置了Id属性,将发出PUT。您可以这样更改Id属性。我将在两个实例中对您的模型进行console.log,并确保已设置levant Id属性
奇怪的是,它在安卓2.x及更高版本的安卓4上运行。如果上面的说明不正确,也许你可以提供更多关于产品设置的代码。这是安卓2浏览器的问题。它缓存GET、POST和PUT(facepalm)请求。
我会
但它只影响GET请求
我的最终解决方案是向url添加nonce
url: function(){ return "rest_entity + $.now(); }
查看“保存”方法您是通过浏览器运行此应用程序,还是将其打包为类似phonegap的内容?如果您是通过浏览器运行此应用程序,可能会创建一个简单的索引和js文件,该文件使用带方法PUT的$.ajax并调用您的API,以便您可以查看服务器日志文件。将生成一个有趣的测试用例。尝试查看网络上很少有与Android 2.X和PUT相关的问题,但都没有用。我不是phonegap应用程序(*.apk)。我想这是某种主干问题,因为第一次保存/更新产品操作(通过PUT请求)就完成了成功。
jQuery.ajaxSetup({
cache: false
});
url: function(){ return "rest_entity + $.now(); }