jQuery .load() not working in IE10

2019-03-06 15:47发布

问题:

I am having trouble with IE 10 and jQuery .load(). I have a PHP page that I load once the website proper has loaded:

$(document).ready( function() { 
    $('#words').html('<div id="loading"><div id="loading-text">Analyzing frequency data...</div><div id="loading-image"><img src="loading.gif"></div></div>');
    $('#words').load('translate.php?character=<?php echo $character1;?>');
}); 

It works fine in Chrome, but in IE the "loading" text appears for about 1 second and disappears, and the translate.php file never loads.

If I change the code to

$(document).ready( function() { 
    $('#words').html('<div id="loading"><div id="loading-text">Analyzing frequency data...</div><div id="loading-image"><img src="loading.gif"></div></div>');
    $('#words').load('index.php');
}); 

it loads fine. However, if I change the code to:

$(document).ready( function() { 
    $('#words').html('<div id="loading"><div id="loading-text">Analyzing frequency data...</div><div id="loading-image"><img src="loading.gif"></div></div>');
    $('#words').load('translate.php?character=章');
}); 

is still doesn't work.

Also, when I echo the character in the translate.php file it shows up with "?" which leads me to believe there's a character encoding problem. However:

  • All pages are encoded "UTF-8 without BOM"
  • I have <meta charset="UTF-8"> in the <head> tags
  • Chinese characters that aren't loaded from translate.php show up fine

Could it be that the jQuery load is stuffing the encoding up in IE only?

I am lost!

Thanks, Dan

回答1:

Try url encoding the character in the url

$(document).ready( function() { 
    $('#words').html('<div id="loading"><div id="loading-text">Analyzing frequency data...</div><div id="loading-image"><img src="loading.gif"></div></div>');
    $('#words').load('translate.php?character='+encodeURIComponent('<?php echo $character1;?>'));
});