Node js http请求:选项中的变量
Node js http request: Variable in Options
如何将变量传递给请求选项?
var test = 'name';
var options = {
method: 'PUT',
url: 'someurl',
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
test: 'Test01', //<- this should be the variable, not the name of the key
description: '\'\''
}
};
我需要通过一些变量动态设置这些键名,但所有 Node 都不接受这些,而是使用 'test' 作为键名。
为此你应该使用 bracket notation
options.form[test] = 'Test01';
所以完整的代码应该是
var test = 'name';
var options = {
method: 'PUT',
url: 'someurl',
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
description: '\'\''
}
};
options.form[test] = 'Test01';
console.log(options);
如何将变量传递给请求选项?
var test = 'name';
var options = {
method: 'PUT',
url: 'someurl',
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
test: 'Test01', //<- this should be the variable, not the name of the key
description: '\'\''
}
};
我需要通过一些变量动态设置这些键名,但所有 Node 都不接受这些,而是使用 'test' 作为键名。
为此你应该使用 bracket notation
options.form[test] = 'Test01';
所以完整的代码应该是
var test = 'name';
var options = {
method: 'PUT',
url: 'someurl',
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
description: '\'\''
}
};
options.form[test] = 'Test01';
console.log(options);