如何在JavaScript和PHP中编码URL? [英] How to encode URL in JavaScript and PHP?

查看:122
本文介绍了如何在JavaScript和PHP中编码URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个名为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆