Monday, June 1, 2009

[jQuery] bug: error of setRequestHeader in IE

hi all:
I come across a bug:
<html>
<head><title>ttt</title>
<script src="/jquery.js"></script>
<script>
$.ajax({
type: 'POST',
url: './',
beforeSend: function(xhr){

xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=utf-8');
},
data: { a: 123 }
});

</script>
</head>
<body>
</body>
</html>
the code above in IE will results in the following header:

Content-Type application/x-www-form-urlencoded,
application/x-www-form-urlencoded;charset=utf-8

I've test with jQuery 1.2.1 and 1.3.2.

the error happened when `type' is 'POST', but not 'GET'.
I check the source code, and find the following code:

3521 // Set the correct header, if data is being sent
3522 if ( s.data )
3523
xhr.setRequestHeader("Content-Type", s.contentType);

when `type' is 'POST', the s.data is not null, and the
`setRequestHeader' is called.
and if setRequestHeader is called twice in IE, the latter one will be
appended after the former.

sorry for my poor English, I hope I've expressed the situation.

No comments: