前端http请求总结

发布: 2016-09-22 16:31:46标签: 前端开发

原生js-ajax使用方法

01varhttp;
02window.XMLHttpRequest?
03http=newXMLHttpRequest():
04http=newAtiveXObject('Microsoft.XMLHTTP');
05
06http.onreadystatechange=function(){
07if(http.readyState==400&&http.status==200){
08//console.log(http.responseText);
09};
10get方式:
11http.open('GET','http://wthrcdn.etouch.cn/weather_mini?city='+cityName,true);
12http.send();
13post方式:
14http.open('POST','ajax.php',true);
15http.setRequestHeader('Content-type','application/x-www-form-urlencodeed');
16http.send('uid=12');
复制代码

jquery方法

01// get方法:
02$.get(
03'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=abcd&cb=?',
04'jsonp'
05).done(function(data){
06console.log(data)})
07// post方法:
08$.post(
09'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?cb=?',
10{'wd':'火影金刚'},
11function(data){
12console.log(data);
13},
14'jsonp'
15);
16
17// $.ajax方法:
18$.ajax({
19url:'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?cb=?',
20type:'GET',
21data:{'wd':'火车票'},
22dataType:'jsonp',
23}).done(function(data){ console.logdata });
复制代码

js-ajax跨域办法

01vars=document.createElement('script');
02s.src='http://www.kuaidi100.com/query?type=yuantong&postid=560598241336&callback=aaa';
03document.body.appendChild(s);
04functionaaa(data){
05console.log(data)
06}
07
复制代码