开发者

jQuery ajax calls (.get, .post., .load etc), return data and IE problem

This is not a question by itself as my notice while working with jQuery 1.3.2

I've spent quite some time researching, looking and finally testing (should have done that in the first place) and here is what I've found:

Let's say you have a small file (call it example.php):

 <div>Hello world</div></div>

And in the main file you make this specific call

<div class="result"></div>
<script type="text/javascript">
$(document).ready(function(){
  $.get('example.php', function(data){
     $('.result').html(data);
  });
});
</script>

Now this will work on every single browser except for IE8/7 (haven't tested it on IE9). That is because the received data is not parsed as html, though it receives it properly. The only thing to do to solve this is to remove the additiona tag from the "example.php" file. This will fix the problem. This might seem simple, but when you've got complex *.php / *.html files on the get call, this could get quite messy and a nightmare to resolve.

NOTE: I tried searching stackoverflow for this specific problem, but did not find a definite answer or solution for this. Hence I've posted this specific problem and solution to it. Considering myself quite a NOOB at this, if anyone points out that there is alrea开发者_Go百科dy an explanation for this, tell me and I will remove this post.


try

$('.result').html("");
$('.result').append(data);

it might remove invalid tags but consider using iframe for container .result


If you need more control over an ajax call using jQuery you should use the $.ajax call itself and manipulate the parameters. Specifically setting the dataType: to 'text'

http://api.jquery.com/jQuery.ajax/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜