zoukankan      html  css  js  c++  java
  • 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实现原理与实现过程:

    学无先后,达者为师
  • 相关阅读:
    bzoj 1856 组合
    bzoj 2809 左偏树平衡树启发式合并
    【HMOI】小C的填数游戏 DP+线段树维护
    【HNOI】 小A的树 tree-dp
    bzoj 1483 链表启发式合并
    bzoj 2733 平衡树启发式合并
    bzoj 2669 状压DP
    bzoj 2165 DP
    【HNOI】 lct tree-dp
    远程debug配置
  • 原文地址:https://www.cnblogs.com/seanchang/p/5443352.html
Copyright © 2011-2022 走看看