一个简单的jsonp例子
现在jsonp很流行看见很多人都写得很复杂,现在给一个简单的调用
首先写一个服务端,创建TestHandler.ashx
[csharp]
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/x-javascript";
string callback = context.Request.QueryString["callback"].Trim();
string name = context.Request.QueryString["name"].Trim();
string str = callback + "([{\"SuitName\":\"" + name + "\"}]);";
context.Response.Write(str);
}
htm的代码:
[html]
<div>
<input type="button" id="btn1" value="Test Handler" />
<div id="msg" style="background-color: Red; width: 100px; height: 50px">
</div>
<script type="text/javascript">
function displayResult(data) {
$("#msg").text(data[0].SuitName);
}
$(function () {
$("#btn1").click(function () {
var url = "TestHandler.ashx?name=majiang&callback=displayResult"
url = "http://starajax.vancl.com/GetSuitsWithLikesBySuitIds?suitIds=9396493&callback=displayResult"
$.getScript(url);
});
});
</script>
</div>
呵呵 这样及可以了 够简单吧
摘自 dz45693的专栏
补充:Web开发 , ASP.Net ,