• jquery ajax 封装方法


         function AjaxMethod() {
                //this.init.apply(this, arguments);
           }
           AjaxMethod.prototype = {

               init: function() {
                   debugger;
               },
               GetJson: function() {
                   jQuery.getJSON(
                     "Json.ashx",
                     { name: 'test', age: 32 },
                      function(data) {
                     debugger;
                     var txt = eval(data);
                      //var obj = data.toJSONString(); //由JSON字符串转换为JSON对象
                     var objs = JSON.stringify(data); //由JSON字符串转换为JSON对象
                          alert(txt);
                      })
               },
               GetAjax: function() {
                   jQuery.ajax({
                       url: "Json.ashx",
                       type: "get",
                       dataType: "json",
                       contextType: "application/json; charset=utf-8",
                       data: { name: 'test', age: 32 },
                       success: function(data) {
                           debugger;
                           jQuery.each(data, function(i) {

                           });

                       },
                       error: function() {
                           //请求出错处理
                           alert(1);
                       }
                   })
               },
               PostAjax: function() {
                   jQuery.post(
                    "Json.ashx",
                    {
                        name: userName,
                        age: 12
                        // ajaxMethod: "Login"
                    },
                    function(data) {
                        var d = data;
                    },
                    "json"
                    );
               }


           }
      var method=new AjaxMethod();

    后台代码:

    context.Response.ContentType = "text/plain";
               // string data ="{ name:\"ants\",age:24}";//构建的json数据
                string data = "[{name:\"fan\",age:26},{name:\"wang\",age:25}]";//构建的json数据

                //string data = "{name:test}";
                //下面两句是用来测试前台向此页面发出的查询字符
                string querystrname = context.Request.QueryString.GetValues("name")[0];//取查询字符串中namer的值
                string querystage = context.Request.QueryString.GetValues("age")[0];//取查询字符串中age的值
                context.Response.Write(JsonConvert.SerializeObject(data) );

  • 相关阅读:
    TSQL常用查询语句
    团队开发注意事项
    SQLServer2005 XML数据类型操作
    Nunit学习笔记
    服务器架构工具表
    JQuery 做的下拉文本框
    面向对象和结构化程序设计的区别
    AFX_MSG是什么意思
    DECLARE_MESSAGE_MAP()
    AfxBeginThread的介绍/基本用法
  • 原文地址:https://www.cnblogs.com/cxlings/p/3032604.html
Copyright © 2020-2023  润新知