如何编码URL参数? [英] How to encode URL parameters?

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

问题描述

我正在尝试将参数传递给如下所示的网址:

I am trying to pass parameters to a URL which looks like this:

http://www.foobar.com/foo?imageurl=

我希望传递自己生成的参数suchas和image URL另一个API,图片的链接结果为:

and I want to pass the parameters suchas and image URL which is generated itself by another API, and the link for the image turns out as:

http://www.image.com/?username=unknown&password=unknown

但是,当我尝试使用URL时:

However, when I try to use the URL:

http://www.foobar.com/foo?imageurl=http://www.image.com/?username=unknown&password=unknown

它不起作用..

我也尝试在imageURL上使用encodeURI和encodeURIComponents,这也不起作用。

I have also tried using encodeURI and encodeURIComponents on the imageURL, and that too doesn't work.

推荐答案

With PHP

echo urlencode("http://www.image.com/?username=unknown&password=unknown");

结果

http%3A%2F%2Fwww.image.com%2F%3Fusername%3Dunknown%26password%3Dunknown

使用Javascript:

var myUrl = "http://www.image.com/?username=unknown&password=unknown";
var encodedURL= "http://www.foobar.com/foo?imageurl=" + encodeURIComponent(myUrl);

DEMO: http://jsfiddle.net/Lpv53/

这篇关于如何编码URL参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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