维护老项目要用到WebService、JQ
1.创建.ASMX文件
2.
在该文件中输入所需要的数据
[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 WebService1 : System.Web.Services.WebService
{
[WebMethod]
public ArrayList HelloWorld()
{
ArrayList responesList=new ArrayList();
responesList.Add("1");
responesList.Add("2");
return responesList;
}
}
3.前端请求 无论jquery还是js都把返回值设置成json 方便取到
<script src="js/JQuery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// 当ID为"btnOK"的按钮被点击时执行以下代码
$("#btnOK").click(function () {
$.ajax({
type: "POST", // 设置请求类型为POST
url: "https://localhost:44340/WebService1.asmx/HelloWorld", // 设置请求URL
dataType: "json", // 预期服务器返回的数据类型
contentType: "application/json; charset=utf-8", // 设置请求头,告诉服务器消息主体是JSON类型
success: function (response) {
// 请求成功时执行的回调函数
$("#111").html = response.d;
console.log(response.d[0]);
// 在这里可以进一步处理服务器返回的数据
},
error: function (jqXHR, textStatus, errorThrown) {
// 请求失败时执行的回调函数
console.log("Request failed with error:", textStatus, errorThrown);
// 在这里可以处理错误情况
}
});
});
});
</script>
(注:请求时由于webservice时soap协议因此只支持post请求,但是也可以通过其他方式修改成get请求 该文章不做过多介绍,下个文章仔细说明)