ajax與json 擷取資料並在前台使用簡單一實例,ajaxjson

來源:互聯網
上載者:User

ajax與json 擷取資料並在前台使用簡單一實例,ajaxjson

用ajax擷取後台資料,返回json資料,怎麼在前台使用呢?

後台

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',   //接受資料格式          data: 'DataType=SearchCustomer&CustomerID=' + document.getElementById("Text3").value,     //要傳遞的資料          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資料是列表還是單個了,就一條就無需中括弧了

context.Response.Write("{\"name\":無使用者,\"id\":\"0\",\"company\":\"無使用者\"}");$(document).ready(function () {      $("#Button3").click(    function (SucCallback) {      $.ajax(      {        type: "get",        url: 'GridDatas.ashx', //幕後處理程式          dataType: 'json',   //接受資料格式          data: 'DataType=SearchCustomer&CustomerID=' + document.getElementById("Text3").value,     //要傳遞的資料          function (dataJson) {           alert(dataJson.Name);           alert(dataJson.Id);        },        error: function () { alert("error"); }      });    })    })

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.