SuperPlan(13)Winner Seller Server - JSONP Basic Auth

本文介绍了如何在Backbone.js中使用JSONP和基本认证进行跨域请求,包括ViewController和ModelLayer的具体实现。同时讨论了在服务器端使用JSONP的方法,并提出将POST请求改为JSON格式的建议。文章还提到了计划进行的技术整合,如Backbone、RequireJS、Jasmine、PhantomJS、Grunt和Bootstrap等。
SuperPlan(13)Winner Seller Server - JSONP Basic Auth

15. Basic Auth on JSONP Server
If we are using the basic auth, we can do the jsonp client like this.

Here is the View Controller of Backbone.js
…snip...
render: function(){
window.logger.debug("I am going to hit the Nav Bar Template Page.");
var widget = this;

var navBars = new NavBarsModel();

window.logger.debug(" navBars url = " + navBars.url);
navBars.fetch({
data: {},
type: 'GET',
success: function(data, response, options) {
window.logger.debug("NavBarView data = " + data);
var compiledTemplate = _.template( htmlTemplate, { items : data } );
$("#navBar").html(compiledTemplate);
},
error: function(jqXHR, textStatus, errorThrown) {
window.logger.error('error arguments: ', arguments);
window.logger.error(jqXHR + " " + textStatus + " " + errorThrown);
},
complete: function(xhr, textStatus) {
window.logger.debug(textStatus);
}
});
}
…snip…

Here is the Model Layer of Backbone.js

…snip...
var Items = Backbone.Collection.extend({
url: '/navbars',
parse: function(response) {
window.logger.debug("getting NavBars from response=" + response);
return response;
},
sync: function(method, model, options){
options.timeout = 10000;
//json mock
//options.url = 'http://localhost:9000/data' + "/navbars" + ".JSON";
//options.dataType = 'json';

//jsonp
options.dataType = "jsonp";
options.crossDomain = true;
options.url = 'http://' + 'customer' + ':' + 'customer' + '@' + 'localhost:9002/v1/sillycat' + '/navbars';

return Backbone.sync(method, model, options);
}
});
…snip…

It the server side, we also use jsonpwithParameter
…snip...
pathPrefix(Version / BrandCode) { (apiVersion, brandCode) =>
authenticate(BasicAuth(new BrandUserPassAuthenticator(dao), "Realm")) { user =>
path("products") {
jsonpWithParameter("callback") {
complete(HttpBody(`application/json`,
dao.db.withSession {
logger.debug("Hitting the URI navbars with apiVersion=" + apiVersion + ",brandCode=" + brandCode)
DefaultJsonProtocol.listFormat[NavBar].write(dao.NavBars.all).toString
}
))
}
}
}
}
…snip…

It is not nice and pretty to use POST in jsonp. So I will change back to JSON for both the server side and client side.

16. JSON cross domain
come soon...

17. Integration(Backbone + Require + Jasmine + Phantom + Grunt + Bootstrap)
come soon…


References:
integration
http://hdnrnzk.me/2013/01/10/backbone-requirejs-jasmine-phantomjs-and-grunt/
https://github.com/ghiden/backbone-requirejs-jasmine-phantomjs-grunt-setup

superplan 12
http://sillycat.iteye.com/blog/1881952
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值