ajax html vs xml/json responses - performance or other reasons

前端 未结 8 1274
死守一世寂寞
死守一世寂寞 2020-12-05 09:58

I\'ve got a fairly ajax heavy site and some 3k html formatted pages are inserted into the DOM from ajax requests.

What I have been doing is taking the html responses

相关标签:
8条回答
  • 2020-12-05 10:31

    In some cases, AJAX responses need to return more information than just the HTML to be displayed. For example, let's say you are returning a list of the first twenty items from a search. You may need to return the total number of search results to be displayed somewhere else in the DOM. You could try piggybacking the total count in a hidden div, but that can get messy. With JSON, the total count can simply be a field value a structured JSON response.

    0 讨论(0)
  • 2020-12-05 10:43

    Here are a few pros for sending JSON/XML instead of HTML:

    1. If the data is going to ever be used outside of your application HTML might be harder to parse and fit into other structure
    2. JSON can be directly embedded in script tags which allows cross domain AJAX scenarios
    3. JSON/XML preserves the separation of concerns between the server side scripts and views
    4. Reduces bandwidth
    0 讨论(0)
提交回复
热议问题