首先我们要创建好webServices项目文件可以是一个项目也可以是一个单独的文件放在其他项目中。
创建webServices文件的头部,引用依赖将AJAX调用此web服务取消注释。
/// <summary>
/// AddApplyOnline 的摘要说明
/// </summary>
[WebService(Namespace = "http://192.168.1.48:3030/AddApplyOnline.asmx/AddApplyOnlineData")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
接下来是编写webServices方法如:
[SoapHeader("myheader")]
[WebMethod]
public string AddApplyOnlineData(string info)
{
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 info1 = new Dmc_ApplicationRecord();
info1 = JsonConvert.DeserializeObject<Dmc_ApplicationRecord>(info);
#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");
//info1.CustomerName = txtCustomerName;
//info1.Address = txtCompanyAddress;
//info1.ContactPhone = txtPhone;
//info1.ContactName = txtName;
//info1.WeChat = txtWechat;
//info1.ScopeName = txtField;
//info1.Remark = txtRamark;
#endregion
info1.CreateTime = DateTime.Now;
if (string.IsNullOrEmpty(info1.CustomerName) || string.IsNullOrEmpty(info1.ContactName) || string.IsNullOrEmpty(info1.ContactPhone) || string.IsNullOrEmpty(info1.Remark))
{
var resultJson = new
{
cod = -1,
resultText = "申请失败!必填数据不能为空!",
};
resultStr = JsonConvert.SerializeObject(resultJson);
}
else
{
info1.CreateTime = DateTime.Now;
result = _bLL.AddDmc_ApplicationRecord(info1);
if (result)
{
var resultJson = new
{
cod = 1,
resultText = "申请成功!",
};
resultStr = JsonConvert.SerializeObject(resultJson);
}
else
{
var resultJs