开发者

Why does jQuery.ajax() add a parameter to the url?

开发者 https://www.devze.com 2022-12-28 15:39 出处:网络
I have a data fetching method that uses jQuery.ajax() to fetch xml files. /**/data: function() { /* debug */try {

I have a data fetching method that uses jQuery.ajax() to fetch xml files.

/*    */data: function() {                                                                                                                                          
                                                                                                                                                                /* debug */try {
        var url = arguments[0] ;                                                                                                                                
        var type = arguments[1] ;                                                                                                                               
        var scope = arguments[2] ;                                                                                                                              
        var callback = arguments[3] ;                                                                                                               开发者_运维技巧            

        var self = this ;                                                                                                                                       
            if(this.cache[url]) {                                                                                                                               
                callback(this.cache[url]) ;                                                                                                                     
            } else if(!this.cache[url]) {                                                                                                                       

                $.ajax({                                                                                                                                        
                    type: "GET" ,                                                                                                                               
                    url: url ,                                                                                                                                  
                    dataType: type ,                                                                                                                            
                    cache: false ,                                                                                                                              
                    success: function(data) {                                                                                                                   

                            if(type == "text/xml") {                                                                                                                                                                                                                                                                                
                                var myJson = AUX.json ;                                                                                                         
                                var jsonString = myJson.build(data,scope,null) ;                                                                                
                                var jsonObject = $.parseJSON(jsonString) ;                                                                                      
                                self.cache[url] = jsonObject ;                                                                                                  
                                callback(url) ;                                                                                                                 

                            } else if(type == "json") {                                                                                                         

                                self.cache[url] = data ;                                                                                                        
                                callback(url) ;                                                                                                                 

                            }                                                                                                                                   

                    } ,                                                                                                                                         
                    error: function() {                                                                                                                         
                        throw "Ajax call failed." ;                                                                                                             
                    }                                                                                                                                           
                }) ;                                                                                                                                            

            }                                                                                                                                                   
                                                                                                                                                                /* debug */} catch(e) {
                                                                                                                                                                /* debug */     alert("- caller: signTutor.data\n- " + e) ;
                                                                                                                                                                /* debug */}
    } ,                                                                                                                                                         

My problem is: jQuery somehow adds a parameter (?_=1272708280072) to the url if there are escaped (hexadecimal notation) or unescaped utf-8 characters outside of the ASCII range -- i believe -- in the file name. It all works well if the file name does not contain characters in that range.

Type is set to xml so there should not be a confusion of types. Headers of the xml files are also set adequately.

I can see from the console that jQuery throws an error, but I'm not sure as to where the problem really is.

Probably a problem with file name formatting, but I did not find any resources on the web as to AJAX file name specifications. Any ideas?

Thanks for you help!


That is a 'cache-buster' and is ignored.

The added parameter changes the url just enough to bypass most all caches that are between you and the source.

If the Url was not modified, it is likely that data would be served from any one of the caches between you and the resource, including your browser, any proxies, and perhaps the server itself.

You can find a lot of explanations on the net. Here is one.


it should be ignored.

Just to make a test, if you are using rails, don't use the javascript_include_tag but pass the JavaScript as

<script src="/path/for/the/script/script.js" type="text/javascript"></script> 

It won't enable the cache-buster and with that you can see if your problem is where you think that it is.

0

精彩评论

暂无评论...
验证码 换一张
取 消