本文主要和大家分享js如何实现发送http请求,最简单的方法自然是jquery,但是有的网站并没有加载jquery的js,如果直接用控制台写代码就会报函数没有定义的错误,当然可以把 “http://code.jquery.com/jquery-1.11.1.min.js“文件中的内容复制到console,不过也可以用纯的javascript。$.get();
$.post();//examplevar params = {id:1,name:'tom'};
url = '/test_post.php';
$.post(url,params,function(data){
alert(data);//这个data就是test_post.php返回的数据});xmlhttp=new XMLHttpRequest();
例子是刷hostloc 的金币(访问其他用户的空间,可以赚2金币,每天10次机会)
代码//jquery rawfor(var i=1;i<=10;i++)
{
$.get("/space-uid-"+i+".html");
}//time delayconst interval=1000;var page=1;var click_like=function(){
let url="/space-uid-"+page+".html"
$.get(url);
page++;
console.warn("visit:"+url); if(page>10){
interval_id=window.clearInterval(interval_id);
}
}var interval_id = window.setInterval(click_like,interval);//js onlyxmlhttp=new XMLHttpRequest();for(let i=1;i<=10;i++){
url="/space-uid-"+i+".html"
xmlhttp.open("GET",url,true); //第三个参数是同步异步,主线程只能异步
//xmlhttp.onreadystatechange=favorOK;//发送事件后,收到信息了调用函数
xmlhttp.send();
}
相关推荐: