Jsonベース

2067 ワード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace ajax
{
    /// 
    /// Json      
    /// 
    public class Json : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            
            context.Response.ContentType = "text/plain";
            JavaScriptSerializer jss = new JavaScriptSerializer();
           string json= jss.Serialize(new Person() {Age=20,Name="json"});
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    public class Person 
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

  



    
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"/>
    <script type="text/javascript">
        $(function () {
            $("#btn").click(function () {
                $.post("Json.ashx", { "msg": $("#TextArea1").val() }, function (data, status) {
                    if (status != "success") {
                        alert("    !   !");
                        return;
                    }
                    var person = $.parseJSON(data);
                    alert(person.Name);
                });
            });

        });
    
    </script>


    <textarea id="TextArea1" cols="50" rows="5"/><br/>
<input type="button" id="btn" value="  "/>


</code></pre> 
   </div> 
   <p>  </p> 
   <p> </p> 
   <p>  </p> 
  </div> 
  <p>   :https://www.cnblogs.com/blackHorseplan/p/3900818.html</p> 
 </div> 
</div>
                            </div>
                        </div>