JS使用变量名作为数组中的键

时间:2016-07-09 02:34:49

标签: javascript jquery arrays variables

我有一个调用API的jQuery javascript函数。调用的一部分设置对象类型,并将其设置为对象id。设置了正确的功能,但我不知道如何正确设置密钥。 API get" object_type"而不是" itemDetails"对于数据中的参数。

有没有办法将数据数组中的键值设置为itemDetails,因为它是从函数调用的?

function details_api(object_id, object_type){
    $.ajax({
        url: '/api.php',
        type: 'POST',
        data: {
            function: object_type,
            data: {
                object_type: object_id
            }
        },
        error: function() {
          alert('An error occurred connecting to server. Please check your network');
        },
        dataType: 'json',
        success: function(json) {
            if (json.status) {
                console.log(json.data);
             }else{
                alert(json.message);
            }
        }
    });

}
details_api('1', 'itemDetails');

0 个答案:

没有答案