$.ajax() 的使用
1.发送Ajax请求
<button id="btn">发送请求</button>
<script src="/js/jquery.min.js"></script>
<script>
var params = {name: 'wangwu', age: 300}
$('#btn').on('click', function () {
$.ajax({
type: 'post',
url: '/user',
data: {
name: 'lxz',
age: 22
},
data: JSON.stringify(params),
contentType: 'application/json',
success: function (res) {
console.log(res);
}
})
});
</script>
2.发送Jsonp请求
<button id="btn">发送请求</button>
<script src="/js/jquery.min.js"></script>
<script>
function fn (response) {
console.log(response)
}
$('#btn').on('click', function () {
$.ajax({
url: '/jsonp',
jsonp: 'cb',
jsonpCallback: 'fn',
dataType: 'jsonp'
})
});
</script>
app.get('/jsonp', (req, res) => {
const cb = req.query.cb
const data = cb + "({name: 'zhaoliu'})"
res.send(data);
});