代码改变世界

WebService ajax 调用

2020-05-31 09:06  idea555  阅读(453)  评论(0)    收藏  举报

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>测试</title>
<script src="../../Scripts/jquery-3.3.1.min.js"></script>
</head>
<body>
<script type="text/javascript">
//$.ajax({
// contentType: 'application/soap+xml;charset=UTF-8;action="http://webxml.com.cn/toTraditionalChinese"',
// dataType: 'xml',
// type: 'post',
// url: 'http://localhost:8081/WebService1.asmx/HelloWorld',
// data: {},
// success: function (data) {
// console.log($('#dictionary').append(data.d));
// }
//});
$.ajax({
type: "POST", //访问WebService使用Post方式请求
contentType: "application/json", //WebService 会返回Json类型
url: "http://localhost:8081/WebService1.asmx/HelloWorld", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
data: "{}", //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到
dataType: 'json',
success: function (result) { //回调函数,result,返回值
$('#dictionary').append(result.d);
alert(result.d);
}
});
</script>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication18
{
/// <summary>
/// WebService1 的摘要说明
/// </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 WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod(Description ="问候")]
public string SayHello(string name)
{
return "你好," + name;
}

[WebMethod(Description ="两数和")]
public int Add(int a,int b)
{
return a + b;
}
}
}