node.js - 大神们给看看,下面的代码为何一直报错?
PHP中文网
PHP中文网 2017-04-17 14:38:35
0
1
569
 var querystring=require('querystring');
    var https=require('https');
    //post要传递的数据
     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 string = JSON.stringify(body);
    var options = {
    
        url: 'https://api.weixin.qq.com/device/authorize_device?access_token=xIfsQltqcgSCpwqIPtB2aKZmcV2_tFeb-8Q-NkSufkj_7m3Jhk1xs9LJwIa5cbvrY49CdFpYMwy0IJXim-BX5oU3vRX5acw7leuBP2PdHXH5ahMeipZNObdeRM',
        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();

报错:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 127.0.0.1:443
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1061:14)
PHP中文网
PHP中文网

认证高级PHP讲师

全部回覆(1)
小葫芦

首先你得理解報錯,能看懂報錯提示

Error: connect ECONNREFUSED 127.0.0.1:443

這個錯誤的意思是127.0.0.1:443拒絕了你的連線

但程式碼中存取的是https://api.weixin.qq.com,為啥呢?

檢查一下你的/etc/hosts文件,是不是把api.weixin.qq.com綁到了127.0.0.1

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板