asp.net jquery ajax json: Simple example of exchanging data

后端 未结 2 623
失恋的感觉
失恋的感觉 2021-01-13 13:16

(Question resolved with the help of the two reply posts--see below)

I would appreciate help getting a simple example of exchanging data JSON data between a browser (

相关标签:
2条回答
  • 2021-01-13 13:28

    Other resource suggest removing the contentType: 'application/json; charset=utf-8', from the AJAX call:

      $.ajax({ 
                url: "ericHandler.ashx", 
                data: myData, 
                dataType: 'json', 
                type: 'POST', 
                success: function (data) { alert("DIDit = " + data.eric); }, 
                error: function (data, status, jqXHR) { alert("FAILED:" + status); } 
            }); 
    

    Read the values on the server side:

    string myPar = context.Request.Form["par"]; 
    

    You can also try:

    string json = new StreamReader(context.Request.InputStream).ReadToEnd(); 
    

    which was mentioned here: https://stackoverflow.com/a/8714375/139917

    0 讨论(0)
  • 2021-01-13 13:47

    I got this working right away. This is its most basic form:

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    
        <script language="javascript" type="text/javascript">
            $(document).ready(function () {
                $.ajax({
                    url: 'Handler.ashx',
                    type: 'POST',
                    success: function (data) {
                        alert(data);
    
                    },
                    error: function (data) {
                        alert("Error");
                    }
                });
            });
        </script>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
    
        </form>
    </body>
    </html>
    

    Code Behind:

    <%@ WebHandler Language="C#" Class="Handler" %>
    
    using System;
    using System.Web;
    
    public class Handler : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题