jQuery + JSON如何从变量定义键 [英] jQuery + JSON how to define key from variable

查看:235
本文介绍了jQuery + JSON如何从变量定义键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

$.post(
    "/factory/set",{
         key : value
    },
    function(response) {

        });
    }, "json"
);

其中

key = "foo"
value = "bar"

但是服务器总是得到"key"和"bar",有没有办法将密钥设置为变量而不是字符串?

but the server always gets "key" and "bar", is there a way to set the key as variable, not string?

推荐答案

创建对象:

var data = {};

然后设置属性:

data[key] = value;

然后在调用$.post()时使用该对象:

Then use the object in your call to $.post():

$.post(
    "/factory/set",data,
    function(response) {

    }, "json"
);

这篇关于jQuery + JSON如何从变量定义键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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