如何在JavaScript和PHP中编码URL? [英] How to encode URL in JavaScript and PHP?
问题描述
我想在一个名为 surl
的变量中发送POST请求中的URL。我应该如何在JavaScript中对其进行编码并在PHP中对其进行解码?例如, surl
的值可以是 http://www.google.co.in/search?q=javascript+urlencode+w3schools
。
I want to send a URL in a POST request in a variable called surl
. How should I encode it in JavaScript and decode it in PHP? For example, the value of surl
could be http://www.google.co.in/search?q=javascript+urlencode+w3schools
.
编辑
抱歉,我忘记了提到,它不是表单提交而是ajax请求。
Sorry, I forgot to mention, it's not form submission but a ajax request.
推荐答案
使用 encodeURIComponent(uri)
(用于编码)和 decodeURIComponent(uri)
用于解码,
Use encodeURIComponent(uri)
(for encoding) and decodeURIComponent(uri)
for decoding,
例如(编码)。
var uri="http://w3schools.com/my test.asp?name=ståle&car=saab";
document.write(encodeURIComponent(uri));
输出
http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab
http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab
解码留给读者。 : - )
Decoding is left for the reader. :-)
资料来源: http: //www.w3schools.com/jsref/jsref_encodeURIComponent.asp
这篇关于如何在JavaScript和PHP中编码URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!