[代码]header: {[代码][代码] [代码][代码]// auth: {[代码][代码] [代码][代码]// username: "admin",[代码][代码] [代码][代码]// password: "public"[代码][代码] [代码][代码]// }[代码][代码] [代码][代码]'Authorization'[代码][代码]: [代码][代码]'Basic YWRtaW46cHVibGlj'[代码][代码] [代码][代码]},[代码]问题解决了,什么原因不太懂。但是直接在header里面写成Authorization字段,开发者工具和手机是可以识别的。
wx.request Auth问题。[代码]wx.request({[代码][代码] [代码][代码]url:[代码][代码] [代码][代码]"https://admin:password@www.xxxxclients"[代码][代码],[代码][代码] [代码][代码]success: [代码][代码]function[代码][代码](res) {[代码][代码] [代码][代码]console.log(res)[代码][代码] [代码][代码]// that.device = res.data.result.objects;[代码][代码] [代码][代码]// console.log(that.device);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码]http请求需要验证用户名和密码,用上面的写法,在开发者工具里面可以用,能正常返回数据,但是在手机上就报错401,[图片] 新手入门,求大佬们指点。之前也查了一些把用户名和密码写在header里面,但是试了好几个不行,不知道有其他什么解决方法
2018-10-04楼主,请问问题解决了吗,我也是同样的问题。 我也是用的emqtt,nginx代理。不过小程序端我用的是mpvue,mqtt也是paho.mqtt,出现同样的问题。
WebSocket net::ERR_CONNECTION_TIMED_OUTappservice?t=1533630357896:1032 WebSocket connection to 'wss://36.41.188.53/mqtt' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT 我在微信小程序中是用mqtt协议,通过nginx代理监听443端口,在本地测试通过,但是在连接服务器是报上述异常,可以ping通ip,telnet也可以连接服务器也正常,MQ使用的是emqttd,恳请各位大佬指点。 // pages/connect/connect.js var app = getApp(); var MQTT = require("../../utils/paho-mqtt.js"); Page({ /** * 页面的初始数据 */ data: { server_addr: '36.41.188.53', user_name: "test", user_psw: ['1','2','3','4','5','6','7','8','9'], error_message: '', switch_checked: false, btn_loading: false }, server_addr_input: function(e) { //console.log(e); this.setData({ server_addr: e.detail.value }); }, user_name_input: function(e) { //console.log(e); this.setData({ user_name: e.detail.value }); }, user_psw_input: function(e) { //console.log(e); this.setData({ user_psw: e.detail.value }); }, switch_change: function(e) { //console.log(e); this.setData({ switch_checked: e.detail.value }); }, btn_connect: function() { //查看输入是否为空,设置错误信息 if (this.data.server_addr == '' || this.data.server_addr == null) { this.setData({ error_message: "server address can not be empty!" }); return; } if (this.data.switch_checked) { if (this.data.user_name == null || this.data.user_name == '') { this.setData({ error_message: "user name can not be empty!" }); return; } if (this.data.user_psw == null || this.data.user_psw == '') { this.setData({ error_message: "user password can not be empty!" }); return; } } //reset error message if (this.data.error_message) { this.setData({ error_message: '' }); } //在按钮上显示加载标志 this.setData({ btn_loading: true }); var client = new MQTT.Client("ws://" + this.data.server_addr + "/mqtt", "mqttjs" + Math.random().toString(36).substr(2)); var that = this; //connect to MQTT broker var connectOptions = { timeout: 10, useSSL: true, cleanSession: true, keepAliveInterval: 30, reconnect: true, userName: "test", password: "123456789", onSuccess: function() { console.log('connected'); app.globalData.mqtt_client = client; client.onMessageArrived = function(msg) { if (app.globalData.messages != null) { app.globalData.messages = [{ topic: msg.topic, humidity: msg.payloadString.toString().substr(6, 2), temperature: msg.payloadString.toString().substr(15, 2), doorLock: msg.payloadString.toString().substr(24, 1), socket: msg.payloadString.toString().substr(32, 1), }].concat(app.globalData.messages); } } client.onConnectionLost = function(responseObject) { if (typeof app.globalData.onConnectionLost === 'function') { return app.globalData.onConnectionLost(responseObject); } if (responseObject.errorCode !== 0) { console.log("onConnectionLost:" + responseObject.errorMessage); } } //去除按钮上的加载标志 that.setData({ btn_loading: false }); wx.switchTab({ url: '../subscribe/subscribe', }); }, onFailure: function(option) { console.log(option); //去除按钮上的加载标志 that.setData({ btn_loading: false }); wx.showModal({ //title: msg.destinationName, content: option.errorMessage }); } }; if (this.data.switch_checked) { connectOptions.userName = this.data.user_name; connectOptions.password = this.data.user_psw; } client.connect( ); }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { if (app.globalData.mqtt_client != null) { wx.reLaunch({ url: '../subscribe/subscribe', }); return; } } })
2018-09-03