electron 网络请求

1、引入
 主进程:const {net}=require('electron');
 渲染进程:const {net}=require('electron').remote;

2、get请求
	  let request=net.request(url地址)
	
	  request.on('response',(response)=>{  监听响应
	      console.log(response.statusCode);  返回状态码
	      console.log(response.headers);  返回请求头
	
	      response.on('data',(chunk)=>{  获取返回数据
	          console.log(JSON.parse(chunk.toString()));
	      })
		  
		  监听结束
	      response.on('end',()=>{
	        console.log('end');
	        
	      })
	      
	  })
	  
 	 request.end();  结束请求,不结束无法返回数据

代码示例:


const {net}=require('electron').remote;

var view=document.querySelector('.wb');
var sp=document.querySelector('.sp');
var btn=document.querySelector('.obtn');
var gbtn=document.querySelector('.gbtn');
var cbtn=document.querySelector('.cbtn');
var dbtn=document.querySelector('.dbtn');

var proxy;

btn.onclick=function(){
  let request=net.request('http://api.tianapi.com/txapi/ncov/index?key=4a8edfc8ac5eae9b0c5bf08157abba96')

  request.on('response',(response)=>{
      console.log(response.statusCode);
      console.log(response.headers);

      response.on('data',(chunk)=>{
          console.log(JSON.parse(chunk.toString()));
      })

      response.on('end',()=>{
        console.log('end');
        
      })
      
  })
  request.end();
}


gbtn.onclick=function(){

}

cbtn.onclick=function()
{

}

dbtn.onclick=function()
{

}

你可能感兴趣的:(electron桌面应用,electron)