Ajax跨域调用WebApi接口方法,后台json格式传参数

来源:互联网 发布:office of mac 编辑:程序博客网 时间:2024/05/08 23:20

界面调用webapi接口按钮控件:

  <asp:button runat="server" ID="btn" Text="跨域调用接口测试" CssClass="BtnList" OnClick="btn_Click" />

按钮事件:

  protected void btn_Click(object sender, EventArgs e)        {            //json格式模型            Model Mp = new Model();            Mp.Header = "MG_License";            Mp.Flag = "Update";            List<Body> listBody = new List<Body>();            Body bodyOne = new Body();            bodyOne.LicenseCode = "91758dfc-ab2a-48a9-a699-9512d68c921c";            bodyOne.ACcountsID = "016b3f9044de413189a96203ed324abd";            bodyOne.MacAddress = "88-E5-65-E1-01-25";            listBody.Add(bodyOne);            Mp.Body=listBody;            string [] AdditionalContent=new  string[0];                      Mp.Additional = AdditionalContent;            //将模型转换成json格式字符串            string _json = JsonConvert.SerializeObject(Mp);            _json = "[" + _json + "]";            this.WriteAjaxMessage("UseWebApi('" + _json + "');");//因为接口参数的类型为字符串,所以加上''        } 
#region 模型类    public class Model    {        public string Header { get; set; }        public string Flag { get; set; }        public List<Body> Body { get; set; }        public string[] Additional { get; set; }           }    public class Body    {        public string LicenseCode { get; set; }        public string ACcountsID { get; set; }        public string MacAddress { get; set; }    }  #endregion

前台脚本方法:UseWebApi()

 <script type="text/javascript">              function UseWebApi(json2) {                   // var json = '[{"Header":"MG_License","Flag":"Update","Body":[{"LicenseCode":"91758dfc-ab2a-48a9-a699-9512d68c921c","ACcountsID":"016b3f9044de413189a96203ed324abd","MacAddress":"88-E5-65-E1-01-25"}],"Additional":[]}]';               //传值的时候是json字符串,回来的值是json格式。                 $.ajax({                    url: "../../hack/crossdomain/__agent.ashx?url=" + encodeURIComponent("http://ip地址:8080/api/BindBand?param=" + json2),//跨域访问接口,需要使用代理服务(在一般处理程序中执行,此处使用GET代理服务),encodeURIComponent可把字符串作为 URI 组件进行编码                    type: "GET",                    dataType:"json",                  //  async:true,//填不填都可以                                      success: function (data) {                                           //   alert(JSON.stringify(arguments[0][0].Flag));//当function方法里没有参数data时,使用此种方式调用返回来的数据。                        alert(data[0].Flag);//提示的值为success                    },                    error: function (data) {                        //   alert(JSON.stringify(arguments[0][0].Flag));//当function方法里没有参数data时,使用此种方式调用返回来的数据。                        alert(data[0].Flag);//提示的值为warn                    }                });                 }    </script>

代理服务(该处是GET服务),即建一般处理程序__agent.ashx

public void ProcessRequest(HttpContext context)        {            string __oraclerequestaddress = HttpUtility.UrlDecode(context.Request.QueryString["url"]);            System.Net.HttpWebRequest __agentrequest = System.Net.WebRequest.Create(__oraclerequestaddress) as System.Net.HttpWebRequest;            string __remoteresponsetext = string.Empty;            using (System.IO.StreamReader __responsereader = new System.IO.StreamReader(__agentrequest.GetResponse().GetResponseStream()))                __remoteresponsetext = __responsereader.ReadToEnd();            context.Response.Write(__remoteresponsetext);        }

注:需引用jquery.min.js。。。

0 0
原创粉丝点击