各类发送请求方法

  • 2017-11-08
  • 61
  • 0

ajax请求

GET请求:

var url = '/query.php'

var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var req = xmlhttp.responseText;
        document.getElementById("station").innerHTML = req;
    }
}
xmlhttp.open("GET",url,true);
xmlhttp.setRequestHeader("host","baidu.com");
xmlhttp.send();


POST请求

var xmlhttp;

xmlhttp=new XMLHttpRequest();

var payload = "pass_key=11321";
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("POST","/try/ajax/demo_post.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(payload);

Python请求

get请求

import requests
 
headers = {'content-type': 'application/json',
           'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0)'}
 
r = requests.get('https://api.github.com/some/endpoint',  headers=headers)
print(r.text)


POST请求

import requests
 
data = {'some': 'data'}
headers = {'content-type': 'application/json',
           'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0)'}
 
r = requests.post('https://api.github.com/some/endpoint', data=data, headers=headers)
print(r.text)

 

评论

还没有任何评论,你来说两句吧