登录
注册
node.js 学习社区
大家看看这段代码有什么问题?为何一直报错?

路漫漫兮修远

2016-08-10 11:15

var querystring=require('querystring');


var https=require('https');


var body =querystring.stringify({

"device_num":"1",

"device_list":[

{

"id":"gh_c3f41afcc90a_f17959764f51b772",

"mac":"C89346A0EFF2",

"connect_protocol":"3",

"auth_key":"",

"close_strategy":"1",

"conn_strategy":"1",

"crypt_method":"0",

"auth_ver":"1",

"manu_mac_pos":"-1",

"ser_mac_pos":"-2",

"ble_simple_protocol": "0"

}

],

"op_type":"1"

});

//var url= 'https://api.weixin.qq.com/device/authorize_device?access_token=j5TLEs-8Otp8svBiYtORTDBrygtXhbeD85MCJFJc1hk3LBN2Lz6cHB2vfMxIv7d-gQmM_dZoVBdhCSuSNBF-uKjOmwRPjO1thItvq4N2HaESRQgA';

//var string = JSON.stringify(body);

var options = {


url: 'https://api.weixin.qq.com/device/authorize_device?access_token=xIfsQltqcgSCpwqIPtB2aKZmcV2_tFeb-8Q-NkSufkj_7m3Jhk1xs9LJwIa5cbvrY49CdFpYMwy0IJXim-BX5oU3vRX5acw7leuBP2PdHXH5ahMeipZNObdeRMbxWLE9KGLbACAUQV',

method: 'POST',

headers: {

"Content-Type": 'application/x-www-form-urlencoded',

"Content-Length": body.length

}


};


function callback(res){



console.log(res.headers);

console.log(res.statusCode)

res.on('data',function(data){

console.log(data.toString());

});


}

var req=https.request(options,callback);


req.write(body);

req.end();

throw er; // Unhandled 'error' event

^Error: connect ECONNREFUSED 127.0.0.1:443

回复 · 1

  • 因为https.request()里面的参数格式不是url啊,
    把options改成

    var options = {

    hostname: "api.mch.weixin.qq.com",

    port: 443,

    path: "/device/authorize_device?access_token=xIfsQltqcgSCpwqIPtB2aKZmcV2_tFeb-8Q-NkSufkj_7m3Jhk1xs9LJwIa5cbvrY49CdFpYMwy0IJXim-BX5oU3vRX5acw7leuBP2PdHXH5ahMeipZNObdeRMbxWLE9KGLbACAUQV",

    method: "POST",

    headers: {

    "Content-Type": 'application/x-www-form-urlencoded',


    "Content-Length": body.length

    }

    };

    url那种参数格式是request模块的,是简化了https.request

    0

发表回复

你可以在回复中 @ 其他人