jQuery ajax call to url containing accent character sends bad Uri from IE to server
I was having trouble sending up a url containing accent characters using IE. Here's a simple function:
function runjQueryTest(){
var url = "/test/Beyoncé/";
$.get( url, function(){});
}
On the server (PHP) I record the value of the request uri ($_SERVER["REQUEST_URI"])
and I see a difference between what FF/Chrome send up versus what IE sends up.
Chrome and FireFox cause the value of ($_SERVER["REQUEST_URI"])
to be
/test/Beyonc%C3%A9/
but requests from IE 8 show the value of ($_SERVER["REQUEST_URI"])
to be
/test/Beyonc\xe9
This is causing my dispatcher's regular expression handler to not match correctly on the server.
Any ideas what the root issue开发者_如何学C here is, and how I can fix it for IE?
Thanks!
I think the solution to your problem is to url encode the characters prior to using them in your url. This will give you a common base across all the browsers.
Without downloading any extension to jquery or use any server-side code, according to w3, you can do:
function runjQueryTest(){
var url = encodeURI("/test/Beyoncé/");
$.get( url, function(){});
}
Just call urldecode on the string :)
<?= urldecode("/test/Beyonc\xe9");?>
/test/Beyoncé
I would think you need to manually URLEncode the string. Try this short extension: http://plugins.jquery.com/project/URLEncode
Usage:
alert( $.URLEncode("This is a \"test\"; or (if you like) an example...");
Output
This%20is%20a%20%22test%22%3B%20or%20%28if%20you%20like%29%20an%20example...
精彩评论