aardio的post和一些注意点
编辑时间:2017-11-25 作者:金满斗 浏览量:5666 来源:原创

用aardio的whttp库访问网页,有的网页会做一些小限制。则必须加入一些头。

通用的post。

一般postdata = inet.url.stringifyParameters(data)转下,然后http.post就好了。注意data是普通aardio里的数据表

有的网页后台限制的是判断是不是ajax访问。就必须加入头文件

var header ='Content-Type:application/json\r\nX-Requested-With:XMLHttpRequest'

postdata = web.json.stringify(data);

提交就是 html =  whttp.post(网址,postdata,header);   

其实判断是否是ajax关键是头文件X-Requested-With:XMLHttpRequest 这句。

后面的慢慢添加。

如果要多个隔离,推荐whttp。cookie一般登录时取出来。这是一个常用的登录取cookie

http.headers = 'X-Requested-With:XMLHttpRequest\r\nContent-Type:application/x-www-form-urlencoded; charset=utf-8';
		var poststr = inet.url.stringifyParameters(postdata);
		http.beginRequest(loginurl,"POST");
 		var htm,err,errNo = http.send(poststr) ;
    	html = http.readAll();   //读取所有返回
 		var header = http.readHeader()  
		http.endRequest();         	
		try{ data = web.json.parse(html);
			if(data.status==1) cookietab.set(header);      //登录成功则保存cookie
		}
		return data;

注意头文件里一定要加

Content-Type:application/x-www-form-urlencoded; charset=utf-8

不然服务端那边不能正确解析数据。

那个cookietab是我自用的一个专门管理cookie的库。


当然据说也可以简单的这样取

http.afterSend = function(statusCode,contentLength){

var cookies = http.readHeader("Set-Cookie:");

console.dump(cookies);  

}


但我没试过。

来说两句吧