반응형
1.ClientScript 쪽 소스
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" language="javascript">
function Push() {
$.ajax({
url: "http://192.168.10.102/TestApp/DBManager.asmx/HelloPreset",
data: "{}",
dataType: 'jsonp',
success: function (data) {
alert("성공 : " + data.d);
},
error: function (result) {
alert("에러 : " + result.status + ' ' + result.statusText);
}
});
}
</script>
<!-- 컨트롤 정의 -->
<input id="Button1" type="button" value="button" onclick="Push()" /></h2>
<script type="text/javascript" src="jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" language="javascript">
function Push() {
$.ajax({
url: "http://192.168.10.102/TestApp/DBManager.asmx/HelloPreset",
data: "{}",
dataType: 'jsonp',
success: function (data) {
alert("성공 : " + data.d);
},
error: function (result) {
alert("에러 : " + result.status + ' ' + result.statusText);
}
});
}
</script>
<!-- 컨트롤 정의 -->
<input id="Button1" type="button" value="button" onclick="Push()" /></h2>
2. 웹서비스 쪽 소스
DLL 파일로 아래 ContentTypeHttpModule을 생성 한다.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; 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("DBManager.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); } } }
그 후 DLL파일을 서비스에서 참조추가 하여 참조한다.
Web.config 파일의 <system.web> 하위에 DLL을 참조하도록 아래 부분을 추가 해 준다.
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ContentTypeHttpModule" type="ContentTypeHttpModule.ContentTypeHttpModule, ContentTypeHttpModule"/>
</httpModules>
여기까지!!!~~
반응형
'프로그래밍' 카테고리의 다른 글
[C#] 프로그램 중복 실행 방지 (2) | 2011.11.25 |
---|---|
[Asp.net] JQuery 및 웹서비스를 이용하여 JSONP로 데이터 가져오기 03 (매개변수) (0) | 2011.11.01 |
[Asp.net] JQuery 및 웹서비스를 이용하여 JSONP로 데이터 가져오기 (원리) (0) | 2011.10.31 |
[C#] DataGridView Checkbox Event (0) | 2011.10.29 |
[C#] DataGridView Combobox 사용시 BackColor가 검정색으로 변경되는 문제 (0) | 2011.10.27 |
댓글