简体   繁体   中英

what's the best way to render repeated divs via jquery and ajax?

What i would normally do is generate the html in the webservice then display it on jquery... i bet there's a better way. help!

在您的Web服务中返回json对象,并在jquery中呈现div,您可以使用此chain.js

If you think about it, you'll always come to two conclusions:

  • "pre render" HTML serverside and send the whole package to a browser

    = more traffic over the wire, faster rendering, less client cpu usage

  • sending only data & instructions (eg. JSON)

    = less traffic over the wire, more client cpu usage, possibly slower rendering

So it actually it depends on your needs. How many people are going to access your data, etc.

Here is a typical jQuery AJAX call to a webservice to get div data and how to handle it.

Assumptions:

  • You are passing a divId to the service to get a particular div. You can change the parameters sent in the data variable to meet your own needs, as long as they match the parameters of the WebMethod.
  • You know where you want to put the result: targetLocation

     $ajax({ type: "POST", url: "WebServices/YourService.asmx/GetDivs", data: "{'divToGetId' :'" + divId + "'}", dataType: "json", contentType: 'application/json; charset=utf-8', success: function(json) { var result = eval("(" + json.d + ")"); $(targetLocation).html(result.value); } }); 

Your webservice:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod(EnableSession = true)]
public string GetDivs(string divId)
{
    return DivProvider.GetChildDivs(divId);
}

Your json should be returned as something like:

{"value": "<div>contents of div 1</div><div>contents of div 2</div>"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM