在其他地方调用,alert(MyXMLHttpRequest(myurl, mydata));后是undefined
//上面myurl,mydata,为传入的内容
function MyXMLHttpRequest(myurl, mydata) {
console.log("进入");
var xhr = new plus.net.XMLHttpRequest();
xhr.onreadystatechange = function() {
switch (xhr.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 (xhr.status == 200) {
console.log("xhr请求已成功" xhr.responseText);
var a = JSON.parse(xhr.responseText);
console.log(a);
return a;// 返回值
} else {
plus.nativeUI.toast("发生了通讯错误!");
console.log("xhr请求失败:" xhr.readyState);
}
break;
}
}
xhr.open("POST", myurl);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(mydata);
1 个回复
DCloud_App_Array
MyXMLHttpRequest方法没有返回值,当然提示的是undefined。