开发者

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 th开发者_如何学JAVAen display it on jquery... i bet there's a better way. help!


return json object in your webservice and render the div's in jquery and you may use this 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>"}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜