js跨域訪問

來源:互聯網
上載者:User

標籤:style   class   blog   c   code   java   

用戶端 HTML

 1 <div id="oid"></div> 2 <script type="text/javascript"> 3     //擷取貨號 4 $.ajax({  5   6                 url: "http://192.168.1.191/H.ashx",  7                 type: "GET",  8                 dataType: ‘jsonp‘,  9                 //jsonp的值自訂,如果使用jsoncallback,那麼伺服器端,要返回一個jsoncallback的值對應的對象. 10                 jsonp: ‘jsoncallback‘, 11                 //要傳遞的參數,沒有傳參時,也一定要寫上 12                 data: null, 13                 timeout: 5000, 14                 //返回Json類型 15                 contentType: "application/json;utf-8", 16                 //伺服器段返回的對象包含name,openid. 17                 success: function (result) { 18                     19                     document.getElementById(‘oid‘).innerText=result.name+":"+result.openid;20                 }, 21                 error: function (jqXHR, textStatus, errorThrown) { 22                     alert(textStatus); 23                 } 24             });25     26 </script>

 

服務端 H.ashx

 1 <%@ WebHandler Language="C#" Class="H" %> 2  3 using System; 4 using System.Web; 5  6 public class H : IHttpHandler { 7      8     public void ProcessRequest (HttpContext context) { 9         context.Response.ContentType = "text/plain";10 11         string result = context.Request.QueryString["jsoncallback"] + "({\"name\":\"測試編號為\",\"openid\":\"123456789\"})";12 13         context.Response.Clear();14         context.Response.Write(result);15         context.Response.End();16         17         18     }19  20     public bool IsReusable {21         get {22             return false;23         }24     }25 26 }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.