Home >Web Front-end >JS Tutorial >How to send http request in js
This article mainly shares with you how to send http requests in js. The simplest method is naturally jquery, but some websites do not load js of jquery. If you write code directly using the console, an error that the function is not defined will be reported. Of course, you can copy the contents of the "http://code.jquery.com/jquery-1.11.1.min.js" file to the console, but you can also use pure 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();
The example is to brush hostloc’s gold coins (visit other users’ spaces, you can earn 2 gold coins, 10 opportunities per day)
Code
//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(); }
Related recommendations:
The above is the detailed content of How to send http request in js. For more information, please follow other related articles on the PHP Chinese website!