ios plus.net.XMLHttpRequest 发送数据后台接受不到
安卓平台则没问题
1***@163.com
- 发布:2019-12-11 20:25
- 更新:2021-03-15 21:36
- 阅读:1411
1***@163.com (作者) - 00后菜鸡
证书是没有问题的
这是ajax代码
const useXHR = () => {
const {net: {XMLHttpRequest}} = useH5Plus();
const {getItem} = useStorage();
return ({
method = 'post',
url = '',
data,
type = 'text',
headers = {},
timeout = 30000,
onprogress,
}) => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onerror = function(e) {
console.log(e);
}
xhr.onreadystatechange = () => {
const {
readyState, status, statusText,
// responseText,
response, __requestHeaders__
} = xhr;
// console.log(xhr);
switch(readyState) {
case 0:
// console.log('xhr请求已初始化');
break;
case 1:
// console.log('xhr请求已打开');
break;
case 2:
// console.log('xhr请求已发送');
break;
case 3:
// console.log('xhr请求已响应');
break;
case 4:
if( status === 200 ) {
// console.log( "xhr请求成功:" + responseText );
resolve({
data: response,
reqHeaders: {...__requestHeaders__},
});
} else {
// console.error( "xhr请求失败:" + readyState );
reject(statusText);
}
break;
default :
break;
}
}
xhr.timeout = timeout;
// xhr.open(method, `http://192.168.0.170${url}`, true);
// xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.open(method, `http://192.168.255.20:8080/`, true);
xhr.responseType = type;
if(headers instanceof Object) {
xhr.setRequestHeader('Content-Type', 'text/html');
for(let key in headers) {
const value = headers[key];
xhr.setRequestHeader(key, value);
}
if(!!getItem('token')) {
xhr.setRequestHeader('token', getItem('token'));
}
}
if(typeof(onprogress) === 'function') xhr.onprogress = e => onprogress(e);
if(method === 'post') {
// const fd = new FormData();
// for(let i in data) {
// fd.append(i, data[i]);
// }
// for(const [key, value] of fd) {
// console.log(`${key}: ${value}`);
// }
// xhr.send(fd);
xhr.send(data);
}else {
xhr.send();
}
});
};
这是请求代码
const fetch = useXHR();
fetch({
data: {
loginid: 'ctx',
password: '111'
}
})
.then(res => {
console.log(res);
})
.catch(err => {
setSubmitting()
alert(err);
})
.finally(() => setSubmitting());
这是node后端代码
const http = require('http');
const app = http.createServer((req, res) => {
const {url, method} = req;
if(url === '/') {
let data = '';
console.log(method);
req.on('data', e => data += e);
req.on('end', () => {
data = decodeURI(data);
console.log(data);
});
res.end(JSON.stringify({res: 500, msg: 'error', data: null}));
}
});
app.listen(8080);
后台返回结果:
zengli@cenglideMacBook-Air-257 http % node index
POST
{"loginid":"ctx","password":"111"}
POST
第一次是安卓请求的
第二次是ios请求的
不管是用hbuilder运行还是xcode运行项目 ios这边发送数据都不行
薪火灬不灭
现在解决没?
2021-03-15 21:36
6***@qq.com
谢谢,用你的办法,行了
2023-02-12 18:11