• jsonp解决跨域


     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实现原理与实现过程:

    学无先后,达者为师
  • 相关阅读:
    剑指offer——从尾到头打印链表节点的值
    1, sync_with_stdio(), tie()的应用
    Python基础1:一些小知识汇总
    HTML
    CSS
    周总结
    十三章
    十二章总结
    十一章总结
    第十一章
  • 原文地址:https://www.cnblogs.com/seanchang/p/5443352.html
Copyright © 2020-2023  润新知