0

I've just start working with handlers in asp.net and I've got one question. I have PostRequestPage.aspx page with such structure:

<asp:Label ID="lblTest" Text="Enter request" runat="server"></asp:Label>
    <br />
    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
    <br />
    <span id="error"></span>
    <br />
    <input type="button" id="btnSubmit" name="btnSubmit" value="Send" />

And I send my POST to handler using ajax post like this:

$(document).ready(function () {
            $('#btnSubmit').click(function() {
                var txtEmail = '<%=txtEmail.ClientID%>';
                $.ajax({
                    type: "POST",
                    url: "HandlerWithPOST.ashx",
                    data: $('#' + txtEmail).val(),                       
                    dataType:"json",                   
                });
            });
        })

In handler I get my POST data:

using (var reader = new StreamReader(context.Request.InputStream))
                    {
 String requestFromPost = reader.ReadToEnd();
var result = JsonConvert.DeserializeObject<List<RootObject>>(requestFromPost);
}

Then I process my data and get the response I want to return to PostRequestPage.aspx page :

String jsonResult = JsonConvert.SerializeObject(new { result = results });

The question is how can I show my jsonResult in label,for example?

4

3 回答 3

0

将数据写入响应尝试

context.Response.Write(jsonResult)

也在写集之前

context.Response.ContentType = "application/json";
于 2013-11-08T07:29:23.887 回答
0

您可以在上下文中编写响应,它将在 ajax 命中的成功处理程序中可用,如下所示:

context.Response.Write(jsonResult)

$.ajax({
type: "POST",
url: "HandlerWithPOST.ashx",
data: $('#' + txtEmail).val(),                       
dataType:"json",                   
success: function (data) {
    console.log("your data from handler is")

console.log(data ? data.d : null) }
});

谢谢,阿布舍克 S。

于 2013-11-08T11:06:23.780 回答
0

您可以像这样扩展您的 ajax 方法:

$.ajax({
    type: "POST",
    url: "HandlerWithPOST.ashx",
    data: $('#' + txtEmail).val(),                       
    dataType:"json",                   
    success: function (data) {
        // do some magic with your json data, like populating your label
    }   
});

看看 jQuery.ajax() api:http ://api.jquery.com/jQuery.ajax/

于 2013-11-08T07:24:50.030 回答