zoukankan      html  css  js  c++  java
  • 解决jQuery ajax跨域问题,Google、IE、Firefox亲测有效

    直接上最后的结果吧

    JS:

     1 $.ajax({
     2             type: "GET",
     3             async: false,
     4             crossDomain: true,
     5             url: "www.test.com/TestHandler.ashx",
     6             data: { Id: "1"},
     7             dataType: "jsonp",
     8             jsonp: "callback",
     9             jsonpCallback: "callbackHandler",
    10             success: function (data) {
    11                 //success
    12             },
    13             error: function (x, status, error) {
    14                 //error
    15             }
    16         });
    View Code

    服务端(这里我用的是ashx一般处理程序):

     1 public void ProcessRequest(HttpContext context)
     2         {
     3             JavaScriptSerializer jsonHelper = new JavaScriptSerializer();
     4 
     5             HttpContext.Current.Response.ContentType = "application/json";
     6             HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
     7             HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, Content-Type,Authorization, Accept, X-Requested-With");
     8             HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
     9 
    10             var list=数据;
    11             
    12             if (string.IsNullOrEmpty(context.Request["callback"]))
    13             {
    14                 context.Response.Write(jsonHelper.Serialize(list));
    15             }
    16             else
    17             {
    18                 context.Response.Write(string.Format("{0}({1})", context.Request["callback"], jsonHelper.Serialize(list)));
    19             }
    20         }
    View Code
  • 相关阅读:
    C# 根据年月日获取星期几方法
    C# 程序实现功能目录
    json字符串转泛型集合对象
    mongoDB基本操作
    Mac下安装mongoDB
    Mac下安装redis
    Mac下安装Scrapy
    beautiful Soup实现抓取图片素材
    python os模块常用方法
    转载---关于Spring的69个面试问答
  • 原文地址:https://www.cnblogs.com/doscanner/p/4157401.html
Copyright © 2011-2022 走看看