Jquery 1.5 ajax sending as GET data on POST

强颜欢笑 提交于 2019-12-23 18:58:08

问题


(Sorry about my english, it aint my birth lang) I have a project that uses codeigniter+JqueryUI. I was thinking about upgrading JQuery version to 1.5 mainly because I am using a lot of ajax calls, and any improvement in speed is highly appreciated. So this is my code, wich works fine in JQuery version 1.4.4:

$("#nome_produto").autocomplete({
            source: function( request, response ) {
                $.ajax({
                    async:false,
                    url: "<?php echo site_url("produtos_produto/json_produtos/f") ?>",
                    dataType: "json",
                    type: "POST",
                    data: request,
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                            return {
                                label: item.label,
                                value: item.label,
                                cod: item.cod
                            }
                        }));
                    },
                    beforeSend:function(){
                        $("#nome_produto").toggleClass("loading");
                    },
                    complete:function(){
                        $("#nome_produto").toggleClass("loading");
                    }
                });
            },
            minLenght:3
        });

In Jquery 1.5, I got a 404 error, but the url requested is this: http://myurl.com/produtos_produto/json_produtos/f?callback=JQUERY_hashofnumbers, even though this is a post request. Does anyone knows why it happens?


回答1:


might be related to this ticket: http://bugs.jquery.com/ticket/8084 the quick fix is:

  jQuery.ajaxSetup({ jsonp: null, jsonpCallback: null});

before doing ajax calls




回答2:


check for hidden redirects

in my case I am using Django, where, in general, all URL's end with '/'

If a request is made for a URL not ending in '/' and the resource cannot be found then Django issues a redirect to that same URL with '/' appended (It's a generally useful option in Django).

In my javascript I had accidentally omitted the trailing '/' in my POST request. This resulted in a redirect (to the correct url). However apparantly POST is automatically converted to GET during a redirect (see e.g. https://stackoverflow.com/a/10586852/473285).



来源:https://stackoverflow.com/questions/5263499/jquery-1-5-ajax-sending-as-get-data-on-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!