0
点赞
收藏
分享

微信扫一扫

c#使用ajax传输数据到webservices方法,并获取服务器端返回值

minute_5 2022-01-06 阅读 66

 前端请求可以是对应的本地地址,也可以是发布的webServices地址加方法名

url: "AddApplyOnline.asmx/AddApplyOnlineData",本地地址
url: 'http://192.168.1.48:3030/AddApplyOnline.asmx/AddApplyOnlineData',IIS发布的地址

前端代码示例:

$.ajax({      //ajax post 调用webservice
            type: "POST",
            contentType: 'application/x-www-form-urlencoded',
            url: "AddApplyOnline.asmx/AddApplyOnlineData",
            //url: 'http://192.168.1.48:3030/AddApplyOnline.asmx/AddApplyOnlineData',
            data: {
                UserName: "admin",
                PWD: "13579",
                DataStr: JSON.stringify({
                    CustomerName: txtCustomerName,
                    Address: txtCompanyAddress,
                    ContactName: txtName,
                    ContactPhone: txtPhone,
                    WeChat: txtWechat,
                    ScopeName: sltField,
                    Remark: txtRamark
                })
            },        //encodeURI
            dataType: 'xml',
            success: function (data) {
                var dataJson = JSON.parse(data.documentElement.textContent);
                if (dataJson.cod==1) {
                    parent.layer.msg(dataJson.resultText, { icon: 1 });
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.layer.closeAll();
                } else {
                    parent.layer.msg(dataJson.resultText, { icon: 2 });
                }
            },
            error: function (xhr) {
                parent.layer.msg(xhr.responseText, { icon: 2 });
            }
        })

服务端代码示例: 

[SoapHeader("myheader")]
        [WebMethod]
        public string AddApplyOnlineData()
        {
            bool result = false;
            string resultStr = "";
            try
            {
                myheader.UserId= HttpContext.Current.Request.Form.Get("UserName");
                myheader.UserPW = HttpContext.Current.Request.Form.Get("PWD");
                string msg = "";
                if (!myheader.IsValid(out msg))
                {
                    var resultJson = new
                    {
                        cod = -1,
                        resultText = msg,
                    };
                    resultStr = JsonConvert.SerializeObject(resultJson);
                }
                else
                {
                    Dmc_ApplicationRecord info = new Dmc_ApplicationRecord();
                    string data= HttpContext.Current.Request.Form.Get("DataStr");
                    info = JsonConvert.DeserializeObject<Dmc_ApplicationRecord>(data);
                    #region 表单提交方法
                    //string txtCustomerName = HttpContext.Current.Request.Form.Get("txtCustomerName");
                    //string txtCompanyAddress = HttpContext.Current.Request.Form.Get("txtCompanyAddress");
                    //string txtName = HttpContext.Current.Request.Form.Get("txtName");
                    //string txtPhone = HttpContext.Current.Request.Form.Get("txtPhone");
                    //string txtWechat = HttpContext.Current.Request.Form.Get("txtWechat");
                    //string txtRamark = HttpContext.Current.Request.Form.Get("txtRamark");
                    //string txtField = HttpContext.Current.Request.Form.Get("txtField");
                    //info.CustomerName = txtCustomerName;
                    //info.Address = txtCompanyAddress;
                    //info.ContactPhone = txtPhone;
                    //info.ContactName = txtName;
                    //info.WeChat = txtWechat;
                    //info.ScopeName = txtField;
                    //info.Remark = txtRamark;
                    #endregion
                    info.CreateTime = DateTime.Now;
                    if (string.IsNullOrEmpty(info.CustomerName) || string.IsNullOrEmpty(info.ContactName) || string.IsNullOrEmpty(info.ContactPhone) || string.IsNullOrEmpty(info.Remark))
                    {
                        var resultJson = new
                        {
                            cod = -1,
                            resultText = "申请失败!必填数据不能为空!",
                        };
                        resultStr = JsonConvert.SerializeObject(resultJson);
                    }
                    else
                    {
                        result = _bLL.AddDmc_ApplicationRecord(info);
                        if (result)
                        {
                            var resultJson = new
                            {
                                cod = 1,
                                resultText = "申请成功!",
                            };
                            resultStr = JsonConvert.SerializeObject(resultJson);
                        }
                        else
                        {
                            var resultJson = new
                            {
                                cod = -1,
                                resultText = "申请失败!",
                            };
                            resultStr = JsonConvert.SerializeObject(resultJson);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogCommonHelper.WriteLogInfo(ex, ex.Message);
                var resultJson = new
                {
                    cod = -1,
                    resultText = "申请失败!",
                };
                resultStr = JsonConvert.SerializeObject(resultJson);
            }
            return resultStr;
        }
举报

相关推荐

0 条评论