ASP.NET Generic HTTP Handler (.ashx) supporting JSONP

旧时模样 提交于 2019-12-06 05:04:50

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();

    });
});
NotMe

The only way "cross domain" could potentially become an issue is if you are using some sort of state mechanism (ie: cookies) as part of the call. Which you shouldn't do.

Otherwise, see the this link: ASP.NET - Passing JSON from jQuery to ASHX for info. There are some good code examples to show you what to do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!