JS_0029:JS用 XMLHttpRequest 发送http请求
1,
httpPost(image) { return new Promise((resolve, reject) => { const http = new XMLHttpRequest(); http.onload = () => { try { const msg = JSON.parse(http.responseText); if (http.status === 200) { if (msg.statusCode === 0) { resolve(msg.result); } else { reject(msg); } } else { reject(msg); } } catch (err) { reject(err); } }; http.onerror = (err) => { reject(err); }; http.open('POST', this.recognizeUrl); http.setRequestHeader('Content-Type', 'application/json;Charset=UTF-8'); if (this.token) { // 将云识别认证token写在请求头中 http.setRequestHeader('Authorization', this.token); } http.send(JSON.stringify(image)); }); }
琥珀君的博客