Javascript 从数组中获取变量,然后传递到nodejs请求模块上的url
我有这个数组,需要传递请求url中的所有变量。 我尝试了result.variable1、result['variable1',result[0],但没有任何效果 如何访问数组中的每个变量并传递到urlJavascript 从数组中获取变量,然后传递到nodejs请求模块上的url,javascript,node.js,Javascript,Node.js,我有这个数组,需要传递请求url中的所有变量。 我尝试了result.variable1、result['variable1',result[0],但没有任何效果 如何访问数组中的每个变量并传递到url result.push({variable1: string1, variable2: string2}); request.post({ url: "mydomain.com/text="Hi"+result[variable1]+"\\n
result.push({variable1: string1, variable2: string2});
request.post({
url: "mydomain.com/text="Hi"+result[variable1]+"\\n"+result[variable2]+"Hello!",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
},
rejectUnauthorized: false,//add when working with https sites
requestCert: false,//add when working with https sites
agent: false,//add when working with https sites
form: {
myfield: "myfieldvalue"
}
}, function (response, err, body){
console.log('Body:',JSON.parse(body));
}.bind(this));
result.push{variable1:string1,variable2:string2};
这将导致阵列变为
结果=[{variable1:string1,variable2:string2}]。
因此,如果您想要“variable1”,您需要作为结果[0]访问它。variable1。您在url:property中的引用都是错误的。如果要在字符串内部使用,请使用“作为字符串周围的分隔符。但我不确定为什么您认为URL参数中需要引号。它应该是text=Hi而不是text=Hi您还应该使用encodeURIComponent来确保参数编码正确。您已经将其作为对象推送。现在需要索引以引用该对象,如结果[0],然后点以获取该对象的属性,如结果[0]。变量1等等。。