如何将数组作为URL参数传递? [英] How to pass an array as a URL parameter?

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

问题描述

我想传递一个数组并作为URL参数添加到页面上的链接,因为稍后在服务器端,我需要数组中的值.我该怎么办?

I would like to pass an array and added to a link on my page as a URL parameter, because later on the server side I need the values from the array. How should I do that?

myArray = ['aaa', 'bbb', 'ccc'];

$('#myLink').attr({"href" : '/myLink?array=' + myArray});

我不确定这是否是正确的方法吗?

I am not sure if that is the proper way of doing this?

推荐答案

您可以序列化JSON:

You can serialize the JSON:

myArray = ['aaa', 'bbb', 'ccc'];
var arrStr = encodeURIComponent(JSON.stringify(myArray));
$('#myLink').attr({ href: '/myLink?array=' + arrStr });

如果您的解析(在下一页上)也是通过JavaScript完成的,那么您将相反地使用 json_decode().

If your parsing (on the next page) is done via JavaScript too, you'll conversely use JSON.parse(). In PHP it would be json_decode().

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

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