开发者

Jquery ajax error when upgrading to v1.5

I upgrading from v1.4.4 to v1.5. I think I have found the problem below

$(doc开发者_JAVA技巧ument).ready(function(){

    // Get token on page load.
    update_csrf_token();

The function looks like this... EDITED

function update_csrf_token()
{
    $.ajax({
        type: "GET",
        url: "<?php echo site_url('includes/csrf_token/'); ?>",
        dataType: "json",
        jsonp: false,
        jsonpCallback: "callbackName",
        success: function(data) {
            csrf_token = data.csrf_token;
            return data.csrf_token;
        }
    });  
}

I looked in firebug for the ajax request and it seems to add ?_=1297353567948 to the end of the url which makes the csrf token not generate. How do i get rid of this or is it new to 1.5 and nothing I can do? Thanks


jQuery 1.5 will automatically add a random callback parameter, but you can override it by setting the jsonp and jsonpCallback jQuery ajax settings.

From jQuery Ajax API:

As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }.

The underscore parameter (?_=...) is a parameter containing a changing timestamp thus making the request URL to always appear something that the browser has never seen before and forcing an actual HTTP request. The parameter can be removed by enabling caching by adding cache: true setting to the .ajax() call (or by global settings: jQuery.ajaxSetup({cache:true});. The setting defaults to true with dataTypes script and jsonp.


Please verify your codes and plugins (for example, the old validation plugin, that uses $.ajaxSettings as default options. Remove codes that looks like this:

var settings = $.extend( {}, $.ajaxSettings, settings );

If you can't or don't want to do that, try add:

{ jsonp: null, jsonpCallback: null }

to your settings. So that your code should be:

$.ajax({
        type: "GET",
        url: "<?php echo site_url('includes/csrf_token/'); ?>",
        dataType: "json",
        jsonp: null,
        jsonpCallback: null,
        success: function(data) {
            csrf_token = data.csrf_token;
            return data.csrf_token;
        }
    }); 

Or you can use $ajaxSetup() to set default settings to prevent jsonp callback:

$ajaxSetup({ jsonp: null, jsonpCallback: null });

For detail information, please refer to Ticket #8084.

Note: Documents of the validation plugin in the official jQuery plugin center - http: //plugins.jquery.com/project/validate is outdated (that really bugs me). It covers only version 1.5.5 of the validation plugin. Make sure referring to the home page: jQuery plugin: Validation for latest release.


Set cache:false within your AJAX (don't forget the comma) and that parameter will stop getting appended. If it was jsonp related, you would see something added like:

callback=jQuery171003826577159490807_1332979269727

But the ?_=looks like a timestamp is a sign that jQuery is trying to prevent any client-side caching of your request.


I had the same problems and, after too much googling, I ended up it was because of the jquery.validate.min.js, that forced jsonp calls.

No more problems with the new version of JQuery Validation 1.8

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜