溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#中ajax跨域訪問問題的示例分析

發(fā)布時間:2021-03-10 10:24:17 來源:億速云 閱讀:163 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關C#中ajax跨域訪問問題的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

[跨域]:指的是瀏覽器不能執(zhí)行其他網站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對JavaScript施加的安全限制。所謂同域是指,域名,協(xié)議,端口均相同,不明白沒關系,舉個栗子:例如,我的電腦上有2個服務器 192.168.0.11和192.168.0.12。如果第一個服務器上的頁面要訪問第二個服務器上面的數(shù)據,就叫做跨域?;蛘遠ttp://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("提交失?。≌埳院笤僭?quot;);
    }
   });
  }
 </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為跨域請求回調函數(shù),切記必不可少。獲取方式也可以為context.Request["callback"],
   //對應前端頁面發(fā)起請求的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 節(jié)點下 增加如下配置:

<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跨域訪問問題的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI