Ajax跨域请求

需求:域名a.test.com要ajax请求b.test.com下的一般处理程序

 

1.前端Ajax请求:(域名a.test.com下的)

$.ajax({
    dataType: "jsonp",
    data: { "ajaxMethod": "getusergamesign", "cookieid": cookieid },
    jsonp: "jsonp_callback",  //服务器端接收,用于function名,随便定义
    url: 'http://b.test.com/Ajax/UserGameSign.ashx',  //请求不同域名的地址
    success: _callBack,   //也可以写function(result) {...};
    error: function() {
        alert('服务器内部错误!');
    }
});

var _callBack = function(result) {   //result为返回的值:{"code":0,"msg":"IB"}  
if (result != null) {
    if (result.code == 0) {
        alert(result.msg);
    }
}
};

其他参数:

type : "get", //或post
async:false, //我试了没起到同步的效果

 

2.服务器端:(域名b.test.com下的)

 public class UserGameSign : BaseCommon, IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
                try
                {
                    if (!string.IsNullOrEmpty(context.Request["ajaxMethod"]))
                    {
                        string responseText = "";
                        context.Response.ContentType = "text/plain";
                        string ajaxMethod = context.Request["ajaxMethod"].ToLower();
                        switch (ajaxMethod)
                        {
                            case "getusergamesign":
                                responseText = this.GetUserGameSign(context);
                                break;
                            default:
                                break;
                        }
                        context.Response.Write(responseText); //返回的结果:{"code":0,"msg":"IB"}
                        context.Response.End();
                    }
                }
                catch (Exception ex) //解决此错误:Thread was being aborted. 问题详解>>
                {
                    if (!(ex is System.Threading.ThreadAbortException))
                    {
                        context.Response.Write(ex.Message);
                        context.Response.End();
                    }
                }
        }

        /// <summary>
        /// 获取用户标签
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string GetUserGameSign(HttpContext context)
        {
            string cookieIdstr = context.Request["cookieid"];
            string strFormat = "{{\"code\":{0},\"msg\":\"{1}\"}}";

            //判断是否是jsonp方式请求 
            string jsonp = string.Empty;
            if (!string.IsNullOrEmpty(HttpContext.Current.Request["jsonp_callback"]))
            {
                jsonp = context.Request["jsonp_callback"];
                context.Response.ContentType = "text/javascript";
            }

            User user = new User();
            string userProperty = user.GetUseProperty(cookieId);
            if (!string.IsNullOrEmpty(userProperty))
            {
                if (string.IsNullOrEmpty(jsonp))
                {
                    return string.Format(strFormat, 0, userProperty); //正常形式返回
                }
                else
                {
//jsonp类型的返回 格式:jsonp({"code":0,"msg":"IB"}) 作为前端ajax回调函数的参数
return jsonp + "(" + string.Format(strFormat, 0, userProperty) + ")"; } } else { return string.Format(strFormat, -1, "失败"); } } }

 

解法方案的问题:详细>>

script请求返回JSON实际上是脚本注入。
1.不能设置同步调用(默认异步)
2.不能接受HTTP状态码
3.不能使用POST提交(默认GET)
4.不能发送和接受HTTP头

 

站外扩展阅读:

jquery使用jsonp进行跨域调用

jsonp详解

ajax 和jsonp 不是一码事 细读详解

说说JSON和JSONP,也许你会豁然开朗

 

转载于:https://www.cnblogs.com/zxx193/p/4029185.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值