微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SuperPlan(13)Winner Seller Server - JSONP Basic Auth

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,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐