views:

191

answers:

6

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 JSON format.

Here is my code:


test.php:

<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="jsFile.js"></script>

<form action='_test.php' method='post' class='ajaxform'>
 <input type='text' name='txt' value='Test Text'>
 <input type='submit' value='submit'>
</form>

<div id='testDiv'></div>

_test.php:

<?php
      $arr = array( 'testDiv' => $_POST['txt'] );
      echo json_encode( $arr );
?>

jsFile.js:

jQuery('.ajaxform').live('submit', function(event) {

 $.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;
});

Behavior:


In Firefox, Chrome, Safari:

When I submit the form then value of textfield(txt) is successfully populated in DIV(testDiv) without disturbing whole page.

In IE:

When I submit the form then it just shows json form on screen like this: {"testDiv":"Test Text"}

How to solve this problem in IE?

Thanks.

+1  A: 

probably it won't help, but looking at your code the part that has problems seemsto me more te serialize() than alle the ajax. i found this http://stackoverflow.com/questions/2300293/jquery-serialize-not-working-on-ajax-loaded-dialogues-in-ie not exacly your case but point out that a missing parenthesis in ie might confuse the serialize output.

just my two cents.

Faber75
+1  A: 

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;
});
Reigel
same result when I use `var $this = $(this);`
NAVEED
`id is not defined` error when I used your `$.each()` logic.
NAVEED
my mistake, in `Query.each(data , function(i, val) {`. the `i` should be `id` like this `function(id, val)`. please try.
Reigel
+1  A: 

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.

aularon
+1  A: 

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.

jtp
Strange! I am also checking it in IE8 on Win7 but it is showing json format. Which jquery version are you using.
NAVEED
1.4.2.min.js from Google CDN. PHP 5.2.8 running on Linux.
jtp
+1  A: 
  1. Try changing the selector (to input:submit or :submit)
  2. Add an error handler to the ajax call and see what it says
sje397
@sje397: After reading your answer I thought about using submit() and it helped me to solve my problem because the problem was in capturing the submit event on IE. Accepting your answer but can you add some more detail in it after issue is solved.I have also posted complete solution here. Thanks
NAVEED
@NAVEEN - if changing the selector as I mentioned above works, then ok, but I don't see anything wrong with you accepting your own answer. As Reigel noted, nobody suggested the solution you eventually used - and it is probably the best way to go.
sje397
@sje397: Ok. You are right. Accepted answer helps to figure out the solution easily in future. Thanks.
NAVEED
+1  A: 

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.

NAVEED
LOL! I forgot to asked if you wrapped it in the ready handler... +1 for figuring it out yourself ;)
Reigel
@Reigel: But still it was working with other browsers.
NAVEED
I think it's the change to using the `submit` callback rather than wrapping in `ready` that fixed it - I think previously it was just doing a normal submit, i.e. selector not working.
sje397
@sje397: Combination of **'wrapping in ready'** and **submit()** solved the issue. When I only use submit() without wrapping it in ready() it does not work on any browser. And when I use `jQuery('.ajaxform').live('submit', function(event) {}` in ready() or not in ready() it does not work with IE only.
NAVEED
What I mean is, using the `submit` callback makes the javascript work - previously I think it wasn't being applied at all and normal form submission was happening. Moving the script to the bottom of the page, using the `submit` callback, and not using `ready` would probably have the same effect.
sje397