跨域调用webservice

本人第一次在博客园写博客。

最近研究js的跨域调用,举个小例子。

ASP.net 中webservice 源代码

 

/// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public void GetProduct(string no)
        {
            List<Product> list = new List<Product>();
            
            DataSet ds = GetDs("MYDB", "select ProductCode,fCurQty from StorageTotal where StorageID=1 and CurQty>0 and ProductCode like ‘%" + no + "%‘ order by ProductCode");
           
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                list.Add(new Product() { Code = dr["ProductCode"].ToString(), Qty = Convert.ToInt32(dr["fCurQty"]) });
            }
           
            ResponseResult responseResult = new ResponseResult();
            if (list.Count == 0)
            {
                responseResult.ResponseDetails = "没有查到匹配的纪录!";
                responseResult.ResponseStatus = 0;
            }
            else
            {
                responseResult.ResponseData = list;
                responseResult.ResponseDetails = "查询库存信息成功!";
                responseResult.ResponseStatus = 1;
            }
            string jsoncallback = HttpContext.Current.Request["jsoncallback"];
            string result = jsoncallback + "(" + JsonConvert.SerializeObject(responseResult, Formatting.Indented) + ")";
            HttpContext.Current.Response.Write(result);
            HttpContext.Current.Response.End();
        }

 

  2.html 页面跨域调用方式

  

<script type="text/javascript">        

  var userData = {
            requestUrl: "http://oa.nextorch.com/WebService/Service1.asmx/GetProduct?jsoncallback=?",
            requestParams: { no: null },
            requestCallBack: function (json) {
                if (json.ResponseStatus == 1) {
                    userData.resultData = json.ResponseData;
                    var resultHtml = "";
                    $(userData.resultData).each(function () {
                        resultHtml += "编号" + this.Code;
                        resultHtml += " 数量" + this.Qty;
                        resultHtml += "<br/>";
                    });
                    $(".result").html(resultHtml);
                } else $(".result").html(json.ResponseDetails);
            },
            resultData: null
        };
        $(function () {
            $("#btnSearch").click(function () {
                userData.requestParams.no = $("#txtValue").val();
                $.getJSON(userData.requestUrl, userData.requestParams, userData.requestCallBack);
            });
        });

    </script>

跨域调用webservice,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。