ASP.NET通用HTTP处理程序(ashx的),支持JSONP程序、NET、ASP、HTTP

由网友(温致如猫)分享简介:有人可以显示一个HTTP处理程序,返回JSON和支持跨域调用的一个例子。我使用jQuery的的getJSON()发送一个请求,我的Web服务器上的.ashx的文件。 Can someone show an example of an HTTP Handler that returns JSON and support...

有人可以显示一个HTTP处理程序,返回JSON和支持跨域调用的一个例子。我使用jQuery的的getJSON()发送一个请求,我的Web服务器上的.ashx的文件。

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.

我明白我需要补充的吗?回调=?以我的网址中的getJSON()的URL,但我不知道什么需要我的ashx文件的服务器上做?

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

    });
});
阅读全文

相关推荐

最新文章