用戶端
$('button').eq(0).click(function() {
$.getJSON('http://localhost:9000/jquery-jsonp?callback=?',function(data) {
$('.result').html(`
編号: ${data.id}<br>
消息: ${data.msg}
`)
})
});
伺服器端
// 測試jquery的jsonp服務
app.all('/jquery-jsonp',(request,response) => {
const data = {
id: 1,
msg: '該使用者名已存在'
};
const str = JSON.stringify(data);
// 接受callback參數
let cb = request.query.callback;
response.end(`${cb}(${str})`);
})