Query+ asp. Net background data to the foreground js parsing method

  • 2020-03-30 02:55:31
  • OfStack

Therefore, when parsing background data, we need to make special treatment according to the background data.

Here I use the background is provided by asp.net WCF services, also have ashx general handler. The principle is similar.

The objects we use a lot in C# are entity objects such as: User; There is a List collection, which generally returns a List.

Also more complex are nested objects or list collections. But it doesn't make any difference, just depending on how much data you have,

Or did the background process return the final result directly?

1, entity object: return is the object, in js, is directly and your background code in the class of object data is the same.

For example, the following code is to get an object. You can just get it with its name property.


$.ajax({ 
type: "post", 
dataType: "json", traditional: true, 
data: { oper: "edit", sid: id }, 
url: AjaxUrl, 
success: function (data, textStatus) { 
if (data != null) { 
if (data) { 
$("#name").val(data.Name);  We got the object.  
SetSelectOpertionValue("selectRelation", data.Relation); 
SetSelectOpertionValue("selectaddreason", data.Reason); 
} else { 
$("#btnAdd").attr("disabled", false); $("#btnAdd").text(" The editor "); 
} 
} 
}, 
complete: function 
(XMLHttpRequest, textStatus) { 
}, 
error: function 
(e) { 
$("#btnAdd").attr("disabled", false); $("#btnAdd").text(" The editor "); 
} 
});

2. The returned data is a List of objects: there are many application scenarios.

In js, the corresponding array array. In the array is the object entity that you return. You can use each traversal. Specific reference:

"(link: #)"

The demo:


$.ajax({             type: "post", 
            dataType: "json", traditional: true, 
            data: { oper: "list", lc: ID,nm:$("#searchname").val() }, 
            url:sAjaxUrl, 
            success: function (data, textStatus) {                 if (data != null) { 
                    if (data.Instance==null &data.Instance.length==0) {                         return; 
                    } 
                    else {                            
                        var datalist = data.Instance;                         if (sort == 1) {                              datalist = datalist.sort( 
                                        function (a, b) {                                                
                                            return (a.Id - b.Id);                                         } 
                                    ); 
                        } else {                             datalist = datalist.sort( 
                                       function (a, b) {                                            return (b.Id - a.Id);                                        } 
                                   ); 
                        } 
                        var html = ""; 
                        //Bind the data to the table
                        var tabledata = GetJson(datalist);                          
                    } 
                } 
            }, 
            complete: function (XMLHttpRequest, textStatus) {             }, 
            error: function (e) {                    
            } 
        });


<script type="text/javascript">
//If the return is:
var json = "['2010-4-2','2010-4-1','2010-5-2']";
var dateArray = eval(json);
for(i in dataArray)
{
   document.write(dataArray[i]);
}
</script>

Or:


 $.each(data.comments, function(i, item) {
            $("#info").append(
                    "<div>" + item.id + "</div>" + 
                    "<div>" + item.nickname    + "</div>" +
                    "<div>" + item.content + "</div><hr/>");
        });

3. If it's complex nesting, it's an object. Js will correspond exactly to the background. You just walk through it.

Now, a lot of times, the background returns to the foreground with json. Json can be directly parsed into objects in js.


Related articles: