gpt4 book ai didi

javascript - 循环访问 http 方法数组

转载 作者:行者123 更新时间:2023-12-03 04:19:40 24 4
gpt4 key购买 nike

继续我之前的post ,我遇到了另一个问题:我试图插入一个数组(包含 GET、POST 等)作为 http 方法属性的值,对其进行循环,并测试多个请求。但是通过数组,我得到了 5 个元素(请求方法)并出现 501 错误。我已经尝试了几种解决方案,包括针对数组的特定位置,但没有一个有效......任何帮助将不胜感激。代码:

var http = require('http');


const options = {
hostname: 'www.google.com',
protocol: 'http:',
method: ['GET', 'POST', 'DELETE', 'PUT', 'STARDUST']
}


var callback = function (response) {
var exportJson = JSON.stringify(response.headers);
var arrayData = [];
response.on('data', function (data) {
arrayData += data;

});

response.on('end', function () {
console.log('THE DATA IS ' + arrayData);


});

}

var x;
var req;

function test() {

x = options.method;
console.log(x[0]); //This works, I can read the GET

for (var i = 0; i < x.length; i++) {

req = http.request(x, callback);
//req.x[0]; The console says that doesn't know the 0 property...
req.x;
req.end();

}
}

test();

编辑:控制台错误:

<h2>HTTP Error 500.19 - Internal Server Error</h2>
<h3>The requested page cannot be accessed because the related configuration da
ta for the page is invalid.</h3>

最佳答案

这里有一个错误:

x = options.method;  
console.log(x[0]); //This works, I can read the GET
for (var i = 0; i < x.length; i++) {
req = http.request(x, callback);
//req.x[0]; The console says that doesn't know the 0 property...
req.x;
req.end();
}

x 是一个数组,您尝试将数组作为参数传递给 request 方法,但文档声明它必须是键值对对象或字符串。那么你试图获取 req 的字段 x,但该字段不存在,因此出现异常。

enter image description here

https://nodejs.org/api/http.html#http_http_request_options_callback

关于javascript - 循环访问 http 方法数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44005503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com