1

我正在做一些检查请求的应用程序。我的应用程序假设从 xml 读取参数

这是我的代码:

var JsonView = Backbone.View.extend({

    template: _.template($('#json-view').html()),
    invokeService: function () {

        var _this = this;

        //loading animation
        $.mobile.loading('show', {
            text: 'request sended',
            textVisible: true,
            theme: 'e',
            textonly: false
        });


        _this.prepareParams();

        console.log('params-'+ _this.params);  // params-null
        console.log('url-'+ _this.url);   //  url-/api/wines

        $.ajax({
            type: _this.model.get('action'), //GET or POST or PUT or DELETE verb
            timeout: 2000,
            cache: false,
            url: _this.url,
            data: _this.params,
            contentType: 'application/json; charset=utf-8', // content type sent to server
            dataType: 'json', //Expected data format from server
            processdata: _this.processdata, //True or False
            success: function (data) {//On Successfull service call
                _this.displayJson(data);
            },
            error: function (data, status, error) {
                _this.displayJson({ error: data });
            },
            complete: function (msg) {
                $.mobile.loading('hide');
                _this.$el.trigger('expand');

            }
        })

    },
    prepareParams: function () {
        this.url = this.model.get('path');
        this.processdata = (this.model.get('action') == 'GET');

        //build data of the request from "params":

        this.params = {};

        var beforeparams=[],afterparams='';

        _.each(this.model.get('params').models, function (param) {

            if (param.get('placein')) {
                switch(param.get('placein')){
                   case 'before':
                      beforeparams.push( param.get('defaultValue'));
                      break;
                   case 'after':                      
                      afterparams = afterparams + param.get('name')+'='+ param.get('defaultValue')+'&';                   
                      break;
                }//end of switch                
            }
            else
                this.params[param.get('name')] = param.get('defaultValue').replace(/\'/g, '\\"');


        }, this);//end of each




        //take care of before params:
        if(beforeparams.length>0)this.url =this.checkIfHasLast(this.url ,'/')+ beforeparams.join('/');




        //take care of after params:
        if(afterparams!=''){
          afterparams=afterparams.substring(0,afterparams.length-1);//remove last '&'
          this.url =this.checkIfHasLast(this.url ,'?') +afterparams;
        }

        //take care of usual params:

        //avoid stringify for get
        if((this.model.get('action') !== 'GET' )) this.params=JSON.stringify(this.params);
        //no params
        if(this.model.get('params').models.length==0){ this.params=null; }


        return this.url  ;
    },
    checkIfHasLast: function(url ,charMustBeLast){
        return (url[url.length-1]==charMustBeLast) ? url : url+charMustBeLast;

    },  
    displayJson: function (data) {
        var jsonstring = JSON.stringify(data, undefined, 2);
        var msg = this.syntaxHighlight(jsonstring);
        this.$el.find('pre').html(msg);
    },

    render: function () {

        this.$el.html(this.template());
        this.$el.attr("data-role", "collapsible").attr("data-theme", "c").attr("data-content-theme", "d");


        return this;
    }

});

请求发送,我得到了预期的 json,但在我的开发人员工具中,我可以看到烦人的消息:

XHR finished loading: "http://localhost:8080/api/wines?_=1374143463089"

如果有人能解释这个“_=1374143463089”是从哪里来的,我将不胜感激

4

1 回答 1

1

这个额外的参数来自 option cache:false。这是为了避免缓存请求。

于 2013-07-18T10:46:30.260 回答