Jquery、ajax 和 & 符号难题

发布于 2024-09-15 10:55:05 字数 1894 浏览 8 评论 0原文

我知道我应该对传递给其他任何内容的任何 url 进行编码,因为我读过以下内容: http://www.digitalbart.com/jquery-and-urlencode/

我想要分享我正在收听的当前曲目的当前时间。 所以我安装了优秀的 yoururls 缩短器。 我有一些代码将所有部分组合在一起,并生成以下内容: track=2&time=967

因为我不想让每个人都看到我的私钥,所以我有一个小的 php 文件,它接受输入,并附加以下内容,所以它看起来像这样: http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=http://urltoshorten?track=2&time=967

所以在主页中,我调用 $("div.shorturl").load(loadall); 的 jquery,

然后它会执行一些 CURL,然后 Shorter 返回一个不错的短 URL。

就像这样:

$myurl='http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=' . $theurl;
$ch = curl_init($myurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}
echo $data;

一切都很完美。

除了...缩短的 URL 始终采用 http://urltoshorten?track=2 的形式- & 符号之后的任何内容都被缩短。

我已经尝试将整个URL包装在php的URLencode中,我已经将track=2&time=967包装在encodeURI和encodeURIComponent中,我还尝试将整个URL包装在一个或两个中。 尽管如此,&打破了它,尽管我可以看到提交的网址最后看起来像 track=1%26time%3D5 。

如果我将此版本或什至带有未编码 URL 的“纯”版本粘贴到 yoururls 界面中,或者通过 api 将其作为粘贴到浏览器地址栏中的普通 URL 提交到 yoururls,那么它再次完美运行。

所以这不是你的网址的错,看起来网址编码正确,我唯一能想到的是 CURL 可能吗?

现在,您可能会想“为什么不将 & 替换为 *,然后再次将其转换回来?”。 好的,所以当 url 扩展时,我从中获取值,

var track = $.getUrlVar('track');
var time = $.getUrlVar('time');

这样我可能会丢失时间变量,然后在 * 的位置上做一些查找,然后假设 * 之后的其余内容是时间,但它是有点难看,更重要的是,这并不是正确的做事方式。

如果有人可以帮助我,我将不胜感激。

I know that I should encodeURI any url passed to anything else, because I read this:
http://www.digitalbart.com/jquery-and-urlencode/

I want to share the current time of the current track I am listening to.
So I installed the excellent yoururls shortener.
And I have a bit of code that puts all the bits together, and makes the following:
track=2&time=967

As I don't want everyone seeing my private key, I have a little php file which takes the input, and appends the following, so it looks like this:
http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=http://urltoshorten?track=2&time=967

So in the main page, I call the jquery of $("div.shorturl").load(loadall);

It then does a little bit of CURL and then shortener returns a nice short URL.

Like this:

$myurl='http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=' . $theurl;
$ch = curl_init($myurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}
echo $data;

All perfect.

Except... the URL which is shortened is always in the form of http://urltoshorten?track=2 - anything after the ampersand is shortened.

I have tried wrapping the whole URL in php's URLencode, I've wrapped the track=2&time=967 in both encodeURI and encodeURIComponent, I've evem tried wrapping the whole thing in one or both.
And still, the & breaks it, even though I can see the submitted url looks like track=1%26time%3D5 at the end.

If I paste this or even the "plain" version with the unencoded url either into the yoururls interface, or submit it to the yoururls via the api as a normal URL pasted into the location bar of the browser, again it works perfectly.

So it's not yoururls at fault, it seems like the url is being encoded properly, the only thing I can think of is CURL possibly?

Now at this point you might be thinking "why not replace the & with a * and then convert it back again?".
OK, so when the url is expanded, I get the values from

var track = $.getUrlVar('track');
var time = $.getUrlVar('time');

so I COULD lose the time var, then do a bit of finding on where the * is in track and then assume the rest of anything after * is the time, but it's a bit ugly, and more to the point, it's not really the correct way to do things.

If anyone could help me, it would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2024-09-22 10:55:05

我尝试将整个 URL 包装在 php 的 URLencode 中

这确实是您必须做的(假设“URL”您的意思是内部 URL 作为外部 URL 的组件传递)。每当您将值放入 URL 组件时,无论您设置的值是否为 URL,都需要进行 URL 编码。

$myurl='http://...?...&url='.rawurlencode($theurl);

urlencode() 对于这样的查询参数是可以的,但是 rawurlencode() 对于路径部分也可以,所以除非你真的需要空格来看起来稍微漂亮一些 [+%20],默认情况下我会选择 rawurlencode()。)

这将为您提供一个最终 URL,例如:

http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=http%3A%2F%2Furltoshorten%3Ftrack%3D2%26time%3D967

您应该是能够验证作品。如果没有,则 yourls-api.php 有问题。

I have tried wrapping the whole URL in php's URLencode

That is indeed what you have to do (assuming by ‘URL’ you mean inner URL being passed as a component of the outer URL). Any time you put a value in a URL component, you need to URL-encode, whether the value you're setting is a URL or not.

$myurl='http://...?...&url='.rawurlencode($theurl);

(urlencode() is OK for query parameters like this, but rawurlencode() is also OK for path parts, so unless you really need spaces to look slightly prettier [+ vs %20], I'd go for rawurlencode() by default.)

This will give you a final URL like:

http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=http%3A%2F%2Furltoshorten%3Ftrack%3D2%26time%3D967

Which you should be able to verify works. If it doesn't, there is something wrong with yourls-api.php.

梓梦 2024-09-22 10:55:05

我尝试过将整个 URL 包装在 php 的 URLencode 中,我已将 track=2&time=967 包装在encodeURI 和encodeURIComponent 中,我还尝试过将整个 URL 包装在一个或两个中。尽管如此,&打破了它,尽管我可以看到提交的网址最后看起来像 track=1%26time%3D5。

也许对 HTTP 变量如何工作的解释会对您有所帮助。

如果我得到一个包含以下变量和值的页面:

var1 = Bruce Oxford
var2 = Brandy&Wine
var3 = ➋➌➔            (unicode chars)

我们对 var 名称和 var 值进行 uri 编码,即:

var1 = Bruce+Oxford
var2 = Brandy%26Wine
var3 = %E2%9E%8B%E2%9E%8C%E2%9E%94

我们没有做的是对分隔字符进行编码,那么请求数据将是什么样子上面是:

?var1=Bruce+Oxford&var2=Brandy%26Wine&var3=%E2%9E%8B%E2%9E%8C%E2%9E%94

而不是:

%3Fvar1%3DBruce+Oxford%26var2%3DBrandy%26Wine%26var3%3D%E2%9E%8B%E2%9E%8C%E2%9E%94

这当然只是胡言乱语。

I have tried wrapping the whole URL in php's URLencode, I've wrapped the track=2&time=967 in both encodeURI and encodeURIComponent, I've evem tried wrapping the whole thing in one or both. And still, the & breaks it, even though I can see the submitted url looks like track=1%26time%3D5 at the end.

Maybe an explanation of how HTTP variables work will help you out.

If I'm getting a page with the following variables and values:

var1 = Bruce Oxford
var2 = Brandy&Wine
var3 = ➋➌➔            (unicode chars)

We uri-encode the var name and the value of the var, ie:

var1 = Bruce+Oxford
var2 = Brandy%26Wine
var3 = %E2%9E%8B%E2%9E%8C%E2%9E%94

What we are not doing is encoding the delimiting charecters, so what the request data will look like for the above is:

?var1=Bruce+Oxford&var2=Brandy%26Wine&var3=%E2%9E%8B%E2%9E%8C%E2%9E%94

Rather than:

%3Fvar1%3DBruce+Oxford%26var2%3DBrandy%26Wine%26var3%3D%E2%9E%8B%E2%9E%8C%E2%9E%94

Which is of course just gibberish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文