溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ajax與json 獲取數(shù)據(jù)并在前臺使用簡單實例

發(fā)布時間:2020-09-19 17:35:04 來源:腳本之家 閱讀:130 作者:lqh 欄目:web開發(fā)

用ajax獲取后臺數(shù)據(jù),返回json數(shù)據(jù),怎么在前臺使用呢?

后臺


if (dataType == "SearchCustomer")
        {
          int ID;
          if (Int32.TryParse(CustomerID, out ID))
          {
            string s = GridComputer.GridCustomer.getCustomer(1, 1, ID);
            if (s == null)
            {
              context.Response.ContentType = "text/plain";
              context.Response.Write("[{\"name\":無用戶,\"id\":\"0\",\"company\":\"無用戶\"}]");
            }
            else { context.Response.Write(s); }
          }
 
        } 

前臺


 $(document).ready(function () {
      $("#Button3").click(
    function (SucCallback) {
      $.ajax(
      {
        type: "get",
        url: 'GridDatas.ashx', //后臺處理程序  
        dataType: 'json',   //接受數(shù)據(jù)格式  
        data: 'DataType=SearchCustomer&CustomerID=' + document.getElementById("Text3").value,     //要傳遞的數(shù)據(jù)  
        success:SucCallback,
        error: function () { alert("error"); }
      });
    })
    })

參考代碼

grid.getCustomer(1,2,function (data) {
    var list = '<p>' + tree_GridInfo._name + '的用戶有</p><br>';
    list += '<table id="customers"><tr><th>姓名</th><th>電話</th></tr> ';
    $.each(data, function (i, n) {
      list += '<tr onclick="showUser(' + 1 + ')"><td>';
      list += n.name + '</td>' + '<td>' + n.company;
      list += '</td></tr>';
    });
    $("#SearchResult").html(list)

看你的json數(shù)據(jù)是列表還是單個了,就一條就無需中括號了

context.Response.Write("{\"name\":無用戶,\"id\":\"0\",\"company\":\"無用戶\"}");

$(document).ready(function () {
      $("#Button3").click(
    function (SucCallback) {
      $.ajax(
      {
        type: "get",
        url: 'GridDatas.ashx', //后臺處理程序  
        dataType: 'json',   //接受數(shù)據(jù)格式  
        data: 'DataType=SearchCustomer&CustomerID=' + document.getElementById("Text3").value,     //要傳遞的數(shù)據(jù)  
        function (dataJson) {
           alert(dataJson.Name);
           alert(dataJson.Id);
        },
        error: function () { alert("error"); }
      });
    })
    })

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI