简体   繁体   English

从json格式的aspx页面获取响应,而不是普通的html

[英]get response from aspx page in json format instead of plain html

I want to make an ajax call to server. 我想对服务器进行ajax调用。 I need to fetch the html of page say foo.aspx: Foo.aspx html: 我需要获取页面的html说foo.aspx:Foo.aspx html:

<form>
<div>foo</div>
</form>

I am calling this page from remote page like: 我从远程页面调用此页面,如:

         $.ajax({
             url: '/foo.aspx',
             data: {},
             contentType: 'application/json; charset=utf-8',
             dataType: 'json',
             success: function (response) {

             }
         });

I need the html of this page BUT my requirement is that the response from this page should be JSON format. 我需要这个页面的html但我的要求是这个页面的响应应该是JSON格式。 ie. 即。

{"myresponse": {
  "id": "123",
  "html":<HTML of foo.aspx>,

}} 

I need the html as a json property because I need to set other properties too from foo.aspx code behind? 我需要将html作为json属性,因为我需要从foo.aspx代码后面设置其他属性吗? How can I get response from aspx page in json format instead of plain html? 如何以json格式而不是普通的html从aspx页面获得响应? What is the other approach to get HTML of remote page + other properties too in json format from server? 从服务器获取json格式的远程页面+其他属性的HTML的另一种方法是什么? Should I go for HTTPHandlers? 我应该去HTTPHandlers吗? If yes, how can I get the html of foo.aspx in that HTTPHandler? 如果是,我如何在HTTPHandler中获取foo.aspx的html?

You may want to try overriding the render method of your page : 您可能想尝试覆盖页面的render方法:

    protected override void Render(HtmlTextWriter writer)
    {
        var sw = new System.IO.StringWriter();
        var tw = new HtmlTextWriter(sw);
        base.Render(tw);

        Response.Write(String.Format("{{\"myresponse\": {{  \"id": \"123",\"html\":\"{0}\"}}}}"
        , Server.HtmlEncode(sw.ToString()).Replace("\n"," "));
        Response.Flush();
        Response.End();
    }

Sorry for any syntax error, and for the basic handling of carriage returns. 很抱歉任何语法错误,以及回车的基本处理。 I guess it will be better to buffer the output during page_load (Response.BufferOutput = true) 我想在page_load期间缓冲输出会更好(Response.BufferOutput = true)

I changed jbl's example of overriding the Render method a little bit, this worked perfectly for me. 我改变了jbl的一个重写Render方法的例子,这对我来说非常合适。 I didn't need the HTML-encoding, just needed to apply some formatting and include the content-type 我不需要HTML编码,只需要应用一些格式并包含内容类型

            protected override void Render(HtmlTextWriter writer)
            {
                var sw = new System.IO.StringWriter();
                var tw = new HtmlTextWriter(sw);
                base.Render(tw);

                var html = sw.ToString();            
                html = html.Replace("\n", " ");
                html = html.Replace("\r", " ");
                html = html.Replace("\t", " ");
                var data = html.Replace("\"", "\\\"");
                data = data.Replace("/", "\\/");
                var json = String.Format("{{\"html\":\"{0}\"}}", data);

                Response.ContentType = "application/json";
                Response.Write(json);
                Response.Flush();
                Response.End();
            }

If you need HTML then change the datatype to html. 如果需要HTML,请将数据类型更改为html。 The success event will then return html as the response. 然后,成功事件将返回html作为响应。

Something like: 就像是:

$.ajax({
     url: '/foo.aspx',
     dataType: 'html',
     success: function (data, textStatus, jqXHR) {
            alert(data); //data is html. 
     }
 });​

Basically You have to create an HTTPHandler that wraps Foo.aspx output into your JSON evenlope. 基本上你必须创建一个HTTPHandler,它将Foo.aspx输出包装到你的JSON evenlope中。 HAving that said, JS call would be to url '/MyWebHandler?page=foo.aspx' instead of just /foo.aspx. 如上所述,JS调用将是url'/ MyWebHandler?page=foo.aspx',而不仅仅是/foo.aspx。 Within the handler all you need is just to query your page and dump its output. 在处理程序中,您只需查询页面并转储其输出即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM