ajax请求:
$.ajax({
type: "get",//必须使用get方式
async: false,
url: "http://www.lkhs.cn/ashx/LKHS_SJPush.ashx?dn=" + dn + "&AccessFrom=test",
dataType: "jsonp",//jsonp
jsonp: "jsonpCallback", //传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
jsonpCallback: "success_jsonpCallback", //自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
success: function (json) {
},
error: function () {
}
});
function success_jsonpCallback(msg) {
if (msg) {
if (msg[0].count > 0) {
$('#' + divId).append('<a href="http://www.lkhs.cn/hssj/' + dn + '_' + encodeURIComponent(ln).replace(/%/g, '$') + ly + '.html" target="_blank"><img id="img_bar" src="http://www.lkhs.cn/hs/images/sjts/sj_bar.gif" alt="汇商商机" width="525px" height="75px" /></a>')
if (ly == '0')
$('#' + divId).append('<a href="http://www.lkhs.cn/sjwap/' + dn + '_' + encodeURIComponent(ln).replace(/%/g, '$') + ly + '.html" target="_blank"><img id="img_bar" src="http://www.lkhs.cn/hs/images/sjts/sj_bar.gif" alt="汇商商机" width="525px" height="75px" /></a>')
}
}
}
后台:
if (AccessFrom == "test")
{
string CallJsMethod = Context.Request.Params["jsonpCallback"].ToString();//此处参数值为success_jsonpCallback
try
{
IList<TempletToCustomer> list = _service.GetListByDomainName(DomaninName);
if (list.Count > 0)
{
count = list.Count;
}
json.Append(CallJsMethod + "([{\"count\":" + count + "}])");//注意 返回数据格式
}
catch (Exception)
{
json.Clear();
}
}
jsonp实现原理与实现过程: