Backbone.emulateJSON方法设置为true以处理使用application / json编码的请求,因为旧版Web服务器无法处理编码为application / json的请求。
句法:
Backbone.emulateJSON=true
让我们举个例子。
请参阅以下示例:
<!DOCTYPE html>
<head>
<title>emulateJSON Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
Backbone.emulateJSON = true;
Backbone.sync = function(method, model) {
document.write(method + ": " + JSON.stringify(model));
model.set('id', 1);
};
var Boxer = new Backbone.Model({
firstname:"Vijender", lastname:"Singh"
});
Boxer.save();
Boxer.save({country: "india"});
</script>
</body>
</html>
输出:
将以上代码保存在emulatejson.html文件中,然后在新的浏览器中打开此文件。
评论前必须登录!
注册