zoukankan      html  css  js  c++  java
  • Backbone.js使用jsonp api示例

    MyModel = Backbone.Model.extend({
        url: function() { 
            return '/yourJsonpUrlhere';
        },
    
        // override backbone synch to force a jsonp call
        sync: function(method, model, options) {
            // Default JSON-request options.
            var params = _.extend({
              type:         'GET',
              dataType:     'jsonp',
              url:            model.url()+"?callback=?",
              processData:  false
            }, options);
    
            // Make the request.
            return $.ajax(params);
        },
    
        parse: function(response) {
            // parse can be invoked for fetch and save, in case of save it can be undefined so check before using 
            if (response) {
                if (response.success ) {
                                    // here you write code to parse the model data returned and return it as a js object 
                                    // of attributeName: attributeValue
                                    
                    return {name: response.name};      // just an example,                
                } 
            }
        }
    });
    sync: function(method, model, options) {             
            options.dataType = 'jsonp';
            options.url="http://localhost:8084/CrossDomain_backbone/messages.json?callback=?";           
            //options.contentType='application/json-p';
            options.error=this.errorr;
            return Backbone.sync(method, model, options);
        }
       ,
        parse: function(resp){
            alert('inside parse..');
            return resp.model;
        },
        errorr:function(response,responseText)
        {
            alert('inside callback..: ' + responseText);
        },
    $(function () {
       var Job = Backbone.Model.extend({
          defaults:{
             displayName:'not set'
          }
       });
    
       var AppView = Backbone.View.extend({
          el:$("#hudApp"),
    
          initialize:function () {
             var job = new Job;
             job.url = 'https://jenkins.example.com/jenkins/job/test-job/api/json/?jsonp=?';
             job.fetch({dataType:"jsonp"});
    
             alert(job.get('displayName'));
          });
       });
    
       var app = new AppView;
    });
  • 相关阅读:
    cocos2d-x避免手动修改android.mk文件来编译
    Android.mk详解
    cocos2dx 安卓编译问题收集
    Mac下部署Android开发环境附加NDK
    SpringMVC关于json、xml自动转换的原理研究
    SpringMVC的拦截器
    Spring3中的mvc:interceptors标签配置拦截器
    Spring常用的接口和类(三)
    Spring常用的接口和类(二)
    LeetCode:寻找旋转排序数组中的最小值【153】
  • 原文地址:https://www.cnblogs.com/terrylin/p/2793565.html
Copyright © 2011-2022 走看看