Get background data with ajax, return JSON data, How to use it in the foreground?

Source: Internet
Author: User

Using AJAX to get background data, return JSON data, How to use it in the foreground?

Background

C # code?
123456789101112131415 if(dataType == "SearchCustomer")                {                    intID;                    if(Int32.TryParse(CustomerID, outID))                    {                        strings = 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); }                    }                


Front desk

JavaScript Code?
1234567891011121314  $(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"); }            });        })        })



Reference Code

JavaScript Code?
123456789 grid.getCustomer(1,2,function(data) {        varlist = ‘<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)
======================================================================================= See if your JSON data is a list or a single, There's no need for brackets in one piece.
Context. Response.Write ("{\" name\ ": no user, \" id\ ": \" 0\ ", \" company\ ": \" no user \ "}");

$ (document). Ready (function () {
$ ("#Button3"). Click (
function (succallback) {
$.ajax (
{
Type: "get",
Url: ' Griddatas.ashx ',//background Handler
DataType: ' JSON ',//accept data format
Data: ' datatype=searchcustomer&customerid= ' + document.getElementById ("Text3"). value,//to be passed
function (datajson) {
Alert (datajson.name);
Alert (datajson.id);
},
Error:function () {alert ("error");}
});
})
})

Get background data with ajax, return JSON data, How to use it in the foreground?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.