Home > Web Front-end > JS Tutorial > How to send http request in js

How to send http request in js

小云云
Release: 2018-03-12 16:16:30
Original
6275 people have browsed it

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返回的数据});
Copy after login
xmlhttp=new XMLHttpRequest();
Copy after login

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(); 
}
Copy after login

Related recommendations:

Several ways to share PHP's HTTP requests Detailed explanation and example code of http request encapsulation of the program

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template