C#中關於ajax跨域訪問問題的詳細介紹

黄舟
發布: 2017-05-14 10:49:30
原創
2555 人瀏覽過

最近做項目,需要跨域請求存取資料問題。以下透過本文給大家分享C#中ajax跨域存取程式碼詳解,需要的朋友可以參考下

最近因專案需要,需要跨域請求存取資料。跨域訪問是指什麼?

[跨網域]:指的是瀏覽器不能執行其他網站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對JavaScript施加的安全性限制。所謂同域是指,域名,協議,端口均相同,不明白沒關係,舉個栗子:例如,我的電腦上有2個伺服器 192.168.0.11和192.168.0.12。如果第一個伺服器上的頁面要存取第二個伺服器上面的數據,就叫做跨域。或是http://www.baidu.com 要造訪www.xxx.com也是不同網域也是跨網域。 以下給出完整請求案例:

前端頁面請求代碼片:

<script type="text/javascript">
  function ajaxsubmit(name,phone) {
   $.ajax({
    type: "get",
    url: "http://10.10.10.132:35709/AppInterface/ResourceInsert.ashx",
    data: { "share_name": encodeURI(name), "telphone": encodeURI(phone), "fromtype": 4 },
    dataType : "jsonp",
    jsonp: "callback",
    jsonpCallback: "successcallback",
    success: function (json) {
     alert(json.msg);
    },
    error:function(e){
     alert("提交失败!请稍后再试");
    }
   });
  }
 </script>
登入後複製

一般處理程序代碼片:

public class ResourceInsert : IHttpHandler
 {
  public void ProcessRequest(HttpContext context)
  {
   context.Response.ContentType = "application/json";
   context.Response.ContentEncoding = System.Text.Encoding.UTF8;
   cms.Model.Resource model = new Model.Resource();
   cms.BLL.Resource bll = new BLL.Resource();
   //你所需要进行的操作
   model.share_name = HttpUtility.UrlDecode(context.Request["share_name"]);
   model.ask_telphone = HttpUtility.UrlDecode(context.Request["telphone"]);
   model.back_row_one = context.Request["fromtype"];
   ConvertHelper ch = new ConvertHelper();
   model.share_name = ch.RemoveSpecialChar(model.share_name);
   //successcallback为跨域请求回调函数,切记必不可少。获取方式也可以为context.Request["callback"],
   //对应前端页面发起请求的jsonp和jsonpCallback格式为:jsonp_value=jsonpCallback_value
   if (bll.Exists(model.share_name, model.ask_telphone))
   {
    Message temp = new Message(1, "我们已收到您的请求额!请勿重复提交!", null);
    context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")");
    context.Response.End();
    return;
   }
   else
   {
    if (bll.Add(model) > 0)
    {
     Message temp = new Message(1, "提交成功,我们工作人员会尽快回复你!感谢关注!", null);
     context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")");
     context.Response.End();
     return;
    }
    else
    {
     Message temp = new Message(0, "请确认信息填写无误!", null);
     context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")");
     context.Response.End();
     return;
    }
   }
  }
  public bool IsReusable
  {
   get
   {
    return false;
   }
  }
 }
登入後複製

你以為這裡結束了嗎?當然沒有/斜眼笑。 設定檔中當然不能少,web.config檔中的 system.webServer 節點下 增加如下設定:

<system.webServer>
 <httpProtocol>
  <customHeaders>
  <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
  <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>
  <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>
 </httpProtocol>
 </system.webServer>
登入後複製

以上是C#中關於ajax跨域訪問問題的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!