首页 技术 正文
技术 2022年11月12日
0 收藏 447 点赞 3,267 浏览 1580 个字
 
 
webservice 代码
     /// <summary>
/// MESService 的摘要说明
/// </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 MESService : System.Web.Services.WebService
{ [WebMethod(Description = "")]
public string GetDataReturnStr(string strXML)
{
//你的结果处理
#region 处理数据 #endregion
return Common.JsonHelper.SerializeToJson(new Student() { id = "", name = "测试" });
} }
[Serializable]
public class Student
{
public string id { get; set; }
public string name { get; set; }
}

webservice 文件中需要添加的信息
 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]WebConfig 文件中需要添加节点
1、解决跨域访问的问题
<system.webServer>
<!--解决跨域请求 -->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET" />
<add name="Access-Control-Allow-Headers" value="x-requested-with,content-type" />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
2、解决本地调用成功,外网调用失败的问题。
<system.web>
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</protocols>
</webServices>
</system.web>

 Ajax 代码

 $.ajax({
async: true,
type: "post",
url: "http://localhost:41453/IDataServer/MESService.asmx/GetDataReturnStr",
data: "{strXML:'123123123'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (json) {
var dataObj = eval("(" + json.d + ")");
alert(dataObj.id + '\r\n' + dataObj.name);
},
error: function () { },
complete: function () {
}
});

这样就可以实现 ajax 访问webservice接口了


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905