Add a webservice name "web_service_cross.asmx"
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class web_service_cross : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string Add(string x)
{
x = (Convert.ToInt32(x) + 5).ToString();
return new JavaScriptSerializer().Serialize(x);
}
Add a class file name ContentTypeHttpModule.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
namespace ContentTypeHttpModule
{
public class ContentTypeHttpModule : IHttpModule
{
private const string JSON_CONTENT_TYPE = "application/json; charset=utf-8";
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication app)
{
app.BeginRequest += OnBeginRequest;
app.ReleaseRequestState += OnReleaseRequestState;
}
#endregion
public void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpRequest resquest = app.Request;
if (!resquest.Url.AbsolutePath.Contains("web_service_cross.asmx")) return;
if (string.IsNullOrEmpty(app.Context.Request.ContentType))
{
app.Context.Request.ContentType = JSON_CONTENT_TYPE;
}
}
public void OnReleaseRequestState(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpResponse response = app.Response;
if (app.Context.Request.ContentType != JSON_CONTENT_TYPE) return;
response.Filter = new JsonResponseFilter(response.Filter);
}
}
public class JsonResponseFilter : Stream
{
private readonly Stream _responseStream;
private long _position;
public JsonResponseFilter(Stream responseStream)
{
_responseStream = responseStream;
}
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return true; } }
public override bool CanWrite { get { return true; } }
public override long Length { get { return 0; } }
public override long Position { get { return _position; } set { _position = value; } }
public override void Write(byte[] buffer, int offset, int count)
{
string strBuffer = Encoding.UTF8.GetString(buffer, offset, count);
strBuffer = AppendJsonpCallback(strBuffer, HttpContext.Current.Request);
byte[] data = Encoding.UTF8.GetBytes(strBuffer);
_responseStream.Write(data, 0, data.Length);
}
private string AppendJsonpCallback(string strBuffer, HttpRequest request)
{
return request.Params["callback"] + "(" + strBuffer + ");";
}
public override void Close()
{
_responseStream.Close();
}
public override void Flush()
{
_responseStream.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
return _responseStream.Seek(offset, origin);
}
public override void SetLength(long length)
{
_responseStream.SetLength(length);
}
public override int Read(byte[] buffer, int offset, int count)
{
return _responseStream.Read(buffer, offset, count);
}
}
}
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class web_service_cross : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string Add(string x)
{
x = (Convert.ToInt32(x) + 5).ToString();
return new JavaScriptSerializer().Serialize(x);
}
Add a class file name ContentTypeHttpModule.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
namespace ContentTypeHttpModule
{
public class ContentTypeHttpModule : IHttpModule
{
private const string JSON_CONTENT_TYPE = "application/json; charset=utf-8";
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication app)
{
app.BeginRequest += OnBeginRequest;
app.ReleaseRequestState += OnReleaseRequestState;
}
#endregion
public void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpRequest resquest = app.Request;
if (!resquest.Url.AbsolutePath.Contains("web_service_cross.asmx")) return;
if (string.IsNullOrEmpty(app.Context.Request.ContentType))
{
app.Context.Request.ContentType = JSON_CONTENT_TYPE;
}
}
public void OnReleaseRequestState(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpResponse response = app.Response;
if (app.Context.Request.ContentType != JSON_CONTENT_TYPE) return;
response.Filter = new JsonResponseFilter(response.Filter);
}
}
public class JsonResponseFilter : Stream
{
private readonly Stream _responseStream;
private long _position;
public JsonResponseFilter(Stream responseStream)
{
_responseStream = responseStream;
}
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return true; } }
public override bool CanWrite { get { return true; } }
public override long Length { get { return 0; } }
public override long Position { get { return _position; } set { _position = value; } }
public override void Write(byte[] buffer, int offset, int count)
{
string strBuffer = Encoding.UTF8.GetString(buffer, offset, count);
strBuffer = AppendJsonpCallback(strBuffer, HttpContext.Current.Request);
byte[] data = Encoding.UTF8.GetBytes(strBuffer);
_responseStream.Write(data, 0, data.Length);
}
private string AppendJsonpCallback(string strBuffer, HttpRequest request)
{
return request.Params["callback"] + "(" + strBuffer + ");";
}
public override void Close()
{
_responseStream.Close();
}
public override void Flush()
{
_responseStream.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
return _responseStream.Seek(offset, origin);
}
public override void SetLength(long length)
{
_responseStream.SetLength(length);
}
public override int Read(byte[] buffer, int offset, int count)
{
return _responseStream.Read(buffer, offset, count);
}
}
}
Create a html file
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript">
</script>
<script language="javascript" src="Scripts/json2.js"></script>
<script language="javascript" type="text/javascript">
function click_test() {
$.ajax({ url: "http://localhost:50535/WebSite14/web_service_cross.asmx/Add",//change the url
data: { x: JSON.stringify("10") }, //, y: JSON.stringify("2nd param")
dataType: "jsonp",
success: function (json) {
alert(json.d);
},
error: function () {
alert("Hit error fn!");
}
});
}
</script>
</head>
<body>
<input type="button" value="TEST" onclick="click_test();" />
</body>
</html>
No comments:
Post a Comment