客户端:
$.ajax({
type : "get",
async : true,
url : "http://192.168.77.151:9999/moniter/jsonp?callback=output",
dataType : "jsonp", //jsonp:表示使用jsonp方式
jsonp: "callback", //请求过程中会带上该参数,该参数的值为output
jsonpCallback: "output", //回调函数名,要求服务端返回的字符串数据格式为output(json数据)。
success : function(data) {
alert(data["name"])
},
error : function(errorMsg) {
alert(errorMsg.status)
}
})
服务端采用ngx+lua模拟
local callback = ngx.var.arg_callback //获取客户端发送过来的参数callback的值。
ngx.say(callback.."({\"name\":\"deng\",\"value\":12343})") //返回callback(json数据)
返回数据为"output({"name":"deng","value":12343})"
PS
下方是我个人订阅号,会一直更新各类技术文章,欢迎关注 :)