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) );


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM