展平javascript对象以作为查询字符串传递 [英] Flatten a javascript object to pass as querystring

查看:123
本文介绍了展平javascript对象以作为查询字符串传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript对象,我需要将其压缩成一个字符串,以便我可以作为查询字符串传递,我该怎么做? ie:

I have a javascript object that I need to flatten into a string so that I can pass as querystring, how would I do that? i.e:

{cost:12345,insertBy:'testUser'} 将成为成本= 12345& insertBy = testUser

我不能使用jQuery AJAX调用这个调用,我知道我们可以使用它并传递对象在数据中,但不是在这种情况下。使用jQuery来展平对象也没关系。

I can't use jQuery AJAX call for this call, I know we can use that and pass the object in as data but not in this case. Using jQuery to flatten to object would be okay though.

谢谢。

推荐答案

您想要 jQuery.param

var str = $.param({ cost: 12345, insertBy: 'testUser' });
// "cost=12345&insertBy=testUser"

请注意,这是函数由jQuery内部使用,以序列化作为 data 参数传递的对象。

Note that this is the function used internally by jQuery to serialize objects passed as the data argument.

这篇关于展平javascript对象以作为查询字符串传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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