Jquery或许在前端技术中由于它不太好的性能以及其他原因逐渐被淘汰,但是Jquery在html单页面的应用中依然有着一定的优势,在本章主要简单展示Jquery的两种用于调用接口发起请求的方式。正常情况下,直接采用第一种方式即可,操作性更高,更符合业务需求。
在用Jquery 提供的api方法时,必须先引入Jquery的包,如在html文件中利用script标签引入Jq包
| <script src="https://code.jquery.com/jquery-3.5.1.min.js" async></script> |
| |
复制
js文件中
| import 'https://code.jquery.com/jquery-3.5.1.min.js"' |
复制
一、$.ajax
| $.ajax({ |
| url: 'http://localhost:8000/api/xxx/xxxx', |
| type: 'post', |
| data: '', |
| contentType: 'application/x-www-form-urlencoded', |
| dataType: 'json', |
| success: function (res) { |
| console.log('res', res); |
| }, |
| error: function(err) { |
| console.log('err', err); |
| } |
| }) |
复制
二 、$.getJSON
| $.getJSON('http://localhost:8000/api/xxx/xxxx', function (json) { |
| console.log('json', json); |
| }); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| $.ajax({ |
| url: url, |
| data: data, |
| success: callback, |
| dataType: json |
| }); |
复制