Form submition with jQuery is not working correctly with IE8

后端 未结 5 1529
广开言路
广开言路 2021-01-24 09:40

jQuery $.ajax() does not seem to work correctly with IE8 but it is working with Firefox, Chrome and Safari. I am submittin the form and response back in JSO

相关标签:
5条回答
  • 2021-01-24 10:03

    Your JSON response (output of _test.php) should contain : instead of ;, as @sje397 said. Seems like other browser are okay with ;, but not IE8, so the value is kept a string instead being parsed as an object.

    EDIT: for some reason your data isn't being converted to json, try putting

    if(typeof(data)=='string') {
      data=$.parseJSON(data);
    }
    

    at the top of your success handler and see if it works.

    0 讨论(0)
  • 2021-01-24 10:04
    1. Try changing the selector (to input:submit or :submit)
    2. Add an error handler to the ajax call and see what it says
    0 讨论(0)
  • 2021-01-24 10:07

    instead of

    for(var id in data) {
      jQuery('#' + id).html( data[id] );
    }
    

    have you tried using $.each() ?

    jQuery.each(data , function(id, val) {
      jQuery('#' + id).html( val );
    });
    

    and also, you have misinterpret $(this), (I guess)

    jQuery('.ajaxform').live('submit', function(event) {
    
      var $this = $(this); // add this line...
    
     $.ajax({
            url  : $this.attr('action'),
            type : $this.attr('method'),
            dataType: 'json',
            data : $this.serialize(),
            success : function( data ) {
         for(var id in data) {
          jQuery('#' + id).html( data[id] );
         }
                }
        });
    
     return false;
    });
    
    0 讨论(0)
  • 2021-01-24 10:14

    I replaced my jquery code with this and it is working on all browsers:

    jQuery(document).ready(function(){
    
        jQuery('.ajaxform').submit( function() {
    
            $.ajax({
                url     : $(this).attr('action'),
                type    : $(this).attr('method'),
                dataType: 'json',
                data    : $(this).serialize(),
                success : function( data ) {
                            for(var id in data) {
                                jQuery('#' + id).html( data[id] );
                            }
                          }
            });
    
            return false;
        });
    
    });
    

    I don't know what is this but it is working now. Thanks to all to participate and help me to solve this issues.

    0 讨论(0)
  • 2021-01-24 10:16

    FYI:

    Naveed, I tried the code you posted in IE8 on Win 7 (both in Compatibility and non-Compatibility modes) and it displayed the 'Test Text' correctly as plain text inside the div and not in JSON format.

    0 讨论(0)
提交回复
热议问题