简体   繁体   English

为什么jQuery.ajax()向网址添加参数?

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

I have a data fetching method that uses jQuery.ajax() to fetch xml files. 我有一种使用jQuery.ajax()来获取xml文件的数据获取方法。

/*    */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. 我的问题是:如果文件名中存在ASCII范围之外的转义(十六进制表示法)或未转义utf-8字符,则jQuery以某种方式向URL添加参数( ?_=1272708280072 )。 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. 类型设置为xml因此不应混淆类型。 Headers of the xml files are also set adequately. xml文件的标题也已适当设置。

I can see from the console that jQuery throws an error, but I'm not sure as to where the problem really is. 我可以从控制台中看到jQuery引发错误,但是我不确定问题出在哪里。

Probably a problem with file name formatting, but I did not find any resources on the web as to AJAX file name specifications. 可能是文件名格式出现问题,但是我没有在网上找到有关AJAX文件名规范的任何资源。 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. 添加的参数更改了url,使其恰好绕过了您和源之间的大多数所有缓存。

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. 如果未修改Url,则很可能会从您和资源之间的任何缓存中提供数据,包括您的浏览器,任何代理,甚至可能是服务器本身。

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 只是为了进行测试,如果您使用的是Rails,请不要使用javascript_include_tag而是将JavaScript作为

<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. 它不会启用高速缓存破坏器,因此您可以查看问题是否在您认为的所在位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM