ashx 跨域_ASP.NET通用HTTP处理程序(ashx的),支持JSONP
Can someone show an example of an HTTP Handler that returns JSON and supports cross domain calls. I am using jQuery's getJSON() that sends a request to an .ashx file on my web server.
I understand that I need to add ?callback=? to my url in the getJSON() url, but I'm not sure what needs to be done on the server in my ashx file?
解決方案
Figured it out. I added this function to my handler and called it:
void WriteCallback(HttpContext context, string json)
{
context.Response.Write(string.Format("{0}({1});", context.Request["callback"], json));
}
Then in the browser:
$(function () {
$.getJSON('MyHandler.ashx?callback=?', { Foo: "Bar" }, function (data) {
if (data.SomeCondition)
$('#someElement').show();
});
});
總結(jié)
以上是生活随笔為你收集整理的ashx 跨域_ASP.NET通用HTTP处理程序(ashx的),支持JSONP的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 好看的按钮组件_这个发光的外骨骼盔甲是什
- 下一篇: 面向对象的23种设计模式