在Firefox和Internet Explorer中,url编码的行为不同 [英] url encode behaving differently in Firefox and Internet Explorer
问题描述
我想将用户的条目发送到google的地理编码API。在这样做的时候,我发现了一个问题。
当我通过我的脚本将用户输入(例如köln+ Germany)发送到Firefox中的api时,它的效果非常好。在Internet Explorer中,它不起作用。
I want to send a user's entry to google's geocode API. In doing so, I detected a problem. When I send the user input (e.g. "köln+Germany") through my script to the api in Firefox it works great. In Internet Explorer however it's not working.
以下是我的代码的豁免足以显示问题:
Here's the exempt of my code that's enough to show the problem:
header('Content-type: text/html; charset=UTF-8');
header('Cache-Control: no-cache, must-revalidate');
$loc = urlencode($_GET['loc']);
echo $address = "http://maps.googleapis.com/maps/api/geocode/json?address=$loc&sensor=false";
Firefox中的输出($ address)是: http://maps.googleapis.com/maps/api/geocode/json? address = k%C3%B6ln + Germany& sensor = false (works!)
The output ($address) in Firefox is: http://maps.googleapis.com/maps/api/geocode/json?address=k%C3%B6ln+Germany&sensor=false (works!)
Internet Explorer中的相同: http://maps.googleapis.com/maps/api/geocode/ json?address = k%F6ln + Germany& sensor = false (返回INVALID_REQUEST)
The same in Internet Explorer is: http://maps.googleapis.com/maps/api/geocode/json?address=k%F6ln+Germany&sensor=false (returns "INVALID_REQUEST")
您可以看到ö的编码区别。在Firefox中,它是%C3%B6,在IE中是k%F6。
如果我用户输入k%C3%B6ln + Germany,它的工作原理就像Internet Explorer中的魅力。
You can see the difference in the encoding of the ö. In Firefox it's %C3%B6, in IE it's k%F6. If I make the user input "k%C3%B6ln+Germany" to begin with, it works like a charm in Internet Explorer also.
我用PHP确保我的特殊字符的转换在Internet Explorer中与Firefox相同。所以ö=%C3%B6而不是ö= k%F6
How can I with PHP ensure that the conversion of my special characters is the same in Internet Explorer as in Firefox. So ö = %C3%B6 instead of ö = k%F6
非常感谢!
保罗
推荐答案
这是一个编码问题,Firefox中的默认编码是UTF-8,而在IE中它是一些ISO-XXXX- X,尝试设置
This is an encoding Problem, the default encoding in Firefox is UTF-8 while in IE it is some ISO-XXXX-X, try to set
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
< head> ...< / head> ;
这将将编码设置为UTF-8,因此IE将对Firefox的字符串进行urlencode的编码,因此会生成工作请求。
in Your HTML -<head>...</head>
this will set the encoding to UTF-8, so IE will urlencode the string like Firefox did and therefore produces a working request.
顺便说一下,如果你希望这个链接成为一个链接,你应该提供一个html页面,所以你应该
and by the way, you should deliver a html page if you want that to be a link so you should
echo '<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body>';
在回显< a href = ...> / code>和
before you echo the <a href=...>
and
echo '</body></html>';
之后。
这样IE将使用给定的UTF-8编码显示链接。
That way IE will use the given UTF-8 encoding on displaying the link.
它可能看起来像如果它没有这种东西可以工作,那么IE会解码给定的< a href = ...>
,猜测它是一个链接,然后使自己的html-围绕它的头体显示它 - 其中包括一个< meta http-equiv =content-typecontent =text / html; charset = ISO-XXXX-X> code>
It might seem as if it would work without that stuff, butt then IE decodes the given <a href=...>
, guesses it is a link and then makes his own html-head-body around it to display it - which then includes a <meta http-equiv="content-type" content="text/html; charset=ISO-XXXX-X">
instead.
如果您只是将链接通过AJAX传递到已加载的页面,请确保此页面包含所提及的元标记。
If you just pass the link to via "AJAX" to a page already loaded make sure that this page contains the mentioned meta-tag.
这篇关于在Firefox和Internet Explorer中,url编码的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!