33模块-net【发送网络请求】

XMLHttpRequest模块管理网络请求,与标准HTML中的XMLHttpRequest用途一致,差别在于前者可以进行跨域访问。通过plus.net可获取网络请求管理对象。

可以使用 ajax 来交互,也可以用这个模块 


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>心得</title>
<script type="text/javascript">
//php则使用以下方式接收数据
//配置PHP环境 always_populate_raw_post_data = -1
//$xmldata = file_get_contents("php://input");
//$this->addlogs(json_decode($xmldata,true)['name']);
function one(){
xhr = new plus.net.XMLHttpRequest();
xhr.open( "POST", "http://192.168.31.157/index.php/Login/upfile" );
xhr.setRequestHeader('Content-Type','application/json');
xhr.send({name:'名称',version:'版本'});//若是不行则使用JSON.stringify({name:'名称',version:'版本'});
xhr.onloadend = function(){
if(xhr.statusText=='OK'){
alert(xhr.responseText);
xhr.abort();//取消当前响应,关闭连接并且结束任何未决的网络活动
}
}
}
</script>
</head>
<style>
li{line-height: 40px; height: 40px; border-bottom: #ccc dashed 1px;}
</style>
<body>
<ul>
<li onclick="one()">跨域网络请求对象</li>
</ul>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/xhrs/p/9334995.html
今日推荐