如何使用Jquery从ASP.Net的WebMethod调用返回的HTML? [英] How to return HTML from ASP.Net WebMethod call using Jquery?

查看:77
本文介绍了如何使用Jquery从ASP.Net的WebMethod调用返回的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Asp.Net 4.0 Web窗体和jQuery 1.6.2。
我想打一个Ajax调用页面上的一个WebMethod,并使其返回HTML。
在服务器端将WebMethod看起来是这样的。

Using Asp.Net 4.0 Web Forms and Jquery 1.6.2. I want to make an Ajax call to a WebMethod on a page and have it return html. On the server side the WebMethod looks like this.

[WebMethod]
public static string GetOrders()
{
    return theMethodThatGeneratesHtml();
}

和这里是调用Ajax的功能。

and here is the calling Ajax function.

function GetOrders()
{
    $.ajax({
        type: 'POST',
        contentType: "application/json",
        url: "Orders.aspx/GetOrders",
        data: "{}",
        success: function (data) {
            $('#content').html(data);
        },
        dataType: "text"
    });
}

这是从的WebMethod返回的数据始终包裹起来作为这样开始的JSON对象。

The data that is returned from the WebMethod is always wrapped up as a json object that start like this.

{"d":"\u003ctable\u003e\r\n ........   

我怎样才能获得的WebMethod只是返回HTML?

How can I get the WebMethod to just return HTML?

推荐答案

不管是我设定的数据类型来,它总是返回一个JSON对象包装了一个D(的<​​a href =HTTP:// encosia .COM /一个破变化之间的版本-OF-ASPNET的Ajax />这里是一个解释,为什么返回的数据总是包裹在广告),但JSON对象D只是包装了统一code转义 html的我找了,所以我必须做被改变的Jquery Ajax调用这个

Regardless of what I set the dataType to, it always returns a Json object wrapped up with a "d" (Here is an explanation as to why the data returned is always wrapped in a d) But the Json object "d" just wraps up the Unicode Escaped html I am looking for, so all I have to do is change the Jquery Ajax call to this

function GetOrders()
{
$.ajax({
    type: 'POST',
    contentType: "application/json",
    url: "Orders.aspx/GetOrders",
    data: "{}",
    success: function (data) {
        $('#content').html(data.d);
    },
    dataType: "json"
});
}

和它按预期工作。

这篇关于如何使用Jquery从ASP.Net的WebMethod调用返回的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆