62,241
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
namespace MeierbeiWebDtService
{
/// <summary>
/// WebService 的摘要说明
/// </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 WebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Models.HelloWorldData data = new Models.HelloWorldData();
data.Message = "HelloWorld";
data.Name = "MicJackson";
return js.Serialize(data);
}
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public void GetClass(int fenshu)
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Models.HelloWorldData data = new Models.HelloWorldData();
if (fenshu > 90)
{
data.Message = "优异";
data.Name = "语文";
}
else if (fenshu > 80)
{
data.Message = "良好";
data.Name = "语文";
}
else if (fenshu > 60)
{
data.Message = "及格";
data.Name = "语文";
}
else
{
data.Message = "不及格";
data.Name = "语文";
}
Context.Response.Write(js.Serialize(data));
Context.Response.End();
//return js.Serialize(data);
}
}
}
<html>
<head>
<script src="jquery-1.11.1.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "http://localhost:53568/WebService.asmx/GetClass",
type: "GET",
dataType: "json",
data: {fenshu: 23},
success: function(json) {
alert("success:"+json);
},
error: function(x, e) {
alert("error:"+x.responseText);
},
complete: function(x) {
alert("complete:"+x.responseText);
}
});
$.ajax({
url: "http://localhost:53568/WebService.asmx/HelloWorld",
type: "GET",
dataType: "json",
success: function(json) {
alert("success:"+json);
},
error: function(x, e) {
alert("error:"+x.responseText);
},
complete: function(x) {
alert("complete:"+x.responseText);
}
});
});
</script>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
namespace MeierbeiWebDtService
{
/// <summary>
/// WebService 的摘要说明
/// </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 WebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Models.HelloWorldData data = new Models.HelloWorldData();
data.Message = "HelloWorld";
data.Name = "MicJackson";
return js.Serialize(data);
}
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public void GetClass(int fenshu)
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Models.HelloWorldData data = new Models.HelloWorldData();
if (fenshu > 90)
{
data.Message = "优异";
data.Name = "语文";
}
else if (fenshu > 80)
{
data.Message = "良好";
data.Name = "语文";
}
else if (fenshu > 60)
{
data.Message = "及格";
data.Name = "语文";
}
else
{
data.Message = "不及格";
data.Name = "语文";
}
HttpRequest Request = HttpContext.Current.Request;
string callback = Request["jsonp"];
Context.Response.Write(callback + "(" + js.Serialize(data) + ")");
Context.Response.End();
//return js.Serialize(data);
}
}
}
<html>
<head>
<script src="jquery-1.11.1.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
function callbackjsp(result){
alert("Message:"+result.Message + ",Name:"+result.Name);
}
$(document).ready(function(){
$.ajax({
url: "http://localhost:53568/WebService.asmx/GetClass",
type: "GET",
dataType: "jsonp",
jsonp: "jsonp", //传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
jsonpCallback: "callbackjsp", //自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
data: { fenshu: 93 },
success: function (json) {
alert("success:" + json.Message);
},
error: function (x, e) {
//alert("error:" + x.responseText);
},
complete: function (x) {
//alert("complete:" + x.responseText);
}
});
});
</script>
</body>
</html>