简体   繁体   中英

IE8 and jQuery - problems with trim, length and AJAX

My code looks like this:

function loadData() {

    $.ajax({
        url: "",
        async: true,
        method: 'post',
        dataType: "json",
        data: {
            start: $('input[name=start]').val(),
            stop: $('input[name=end]').val(),
            week: $('#weekpicker').val(),
            range:$('select[name=date_control]').val(),
            type: 'loaddata'
        },
        beforeSend: function(xhr, opts) {
            if (!$('input[name=start]').val() && !$('input[name=end]').val()) {
                bootbox.alert('Select date first!');
                xhr.abort();
                return false;
            }

        },
        success: function(dane) {
            ustawienia = dane.ustawienia;

            // some code here

        },
        error: function(dane) {
            $('button[name=pokaz]').removeAttr('disabled');
            bootbox.alert(dane.responseText);
        }
    });
}

On FF, Chrome and IE>8 everything works fine, problem is with IE8. instead perform the success' function in IE displays an error: 在此处输入图片说明

Line 337 in jQuery is:

globalEval: function( data ) {
    if ( data && jQuery.trim( data ) ) {
        // We use execScript on Internet Explorer
        // We use an anonymous function so that context is window
        // rather than jQuery in Firefox
        ( window.execScript || function( data ) {
            window[ "eval" ].call( window, data );
        } )( data );
    }
},

so I've remove jQuery.trim( data )

and next I have this error: 在此处输入图片说明

// args is for internal usage only
each: function( obj, callback, args ) {
    var value,
        i = 0,
        length = obj.length, //here is line 357!
        isArray = isArraylike( obj );

    if ( args ) {
        if ( isArray ) {
            for ( ; i < length; i++ ) {
                value = callback.apply( obj[ i ], args );

                if ( value === false ) {
                    break;
                }
            }
        } else {
            for ( i in obj ) {
                value = callback.apply( obj[ i ], args );

                if ( value === false ) {
                    break;
                }
            }
        }

    // A special, fast, case for the most common use of each
    } else {
        if ( isArray ) {
            for ( ; i < length; i++ ) {
                value = callback.call( obj[ i ], i, obj[ i ] );

                if ( value === false ) {
                    break;
                }
            }
        } else {
            for ( i in obj ) {
                value = callback.call( obj[ i ], i, obj[ i ] );

                if ( value === false ) {
                    break;
                }
            }
        }
    }

    return obj;
},

What I can do, to make it works?

Had same problem on IE8. Probably your json is not a valid format. You need quotes around all attributes. Official json should have quotes, but nowadays new browsers don't care.

var json = {
    test: "a",  //wrong
    "test": "a" //correct
}

Used this on server side:

JavascriptObject translationsObject = new JavascriptObject();
Dictionary<string, string> translations = Translator.GetTranslations(key.LanguageCode);
foreach (var pair in translations)
{
    if (pair.Value != pair.Key)
        translationsObject.Add("\"" + pair.Key + "\"", pair.Value); //Needed for <=IE8
}
string translationsString = "var translations = ";
translationsString += translationsObject.ToString();
translationsString += ";";
return translationsString;

You are trying to assign the length value to a variable "length". This is a reserved word in Javascript browsers as far as I know

Change it to:

ln = obj.length, //here is line 357!

and change wherever you called "length" to "ln"

example:

 for ( ; i < ln; i++ ) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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