JSON object or String resultset - ajax

I make an ajax call (very simple), that currently returns a string of html. Depending on who is calling it, this string can become very long at times. What I'd like to know is why it's better to return a JSON result and build my HTML afterwards, rather than just returning a long string.

Some advantages of returning JSON instead of HTML:
The data can be used as data for analysis or other uses, not just used for presentation.
JSON data is often/usually much smaller than the full presentation HTML so you are transferring less data over the internet.
You create a separation between data and presentation rather than mix them both into one single API. Your server returns the data which a separate piece of code then turns into presentation.
The JSON data can be processed or modified more easily before presentation (such as filtered, sorted, tagged, expanded/collapsed, etc...).
You can use the same JSON data for many different types of presentation. If you return HTML, the presentation is already baked in so if you want a different presentation, you then have to create a whole new AJAX call.
If you want an extreme way to think about this, then ask yourself why does a database return raw data and not an HTML view of the data? It's because you can do so many more different kinds of things with the actual data so therefore the data is so much more useful by having it give you just the data and then different pieces of code to do something with the data (analyze it, combine it with other data, make decisions based on it, present it for viewing, etc....). If the database only returned an HTML view of the data, it would be far harder to do all these other things with the data. The same is true of an Ajax call which is really just the client's access to data.

Related

MiniProfilerEF view results without RenderIncludes()

Is there another way to view the profiling results of MiniProfiler (I'm specifically interested in EF5 version)?
Every tutorial that I've seen uses MiniProfiler.RenderIncludes(); but since my MVC app mostly returns JSON, that is not an option for me.
Is there a way to write results to file or something like that?
You can read and write results to just about anywhere by changing the MiniProfiler.Settings.Storage to a different IStorage implementation from the default (which stores to http cache). If you wanted to, this could store to and read from a file pretty easily (you would have to write your own custom implementation for that).
The files served by RenderIncludes are the html templates for displaying the results and the script to retrieve the results from the server and render them on the client (all found here). But you are by no means obliged to use this mechanism. If you want to write your own logic for retrieving and displaying results, you should base this off of the logic found in MiniProfilerHandler.GetSingleProfilerResult. This function roughly performs the following (putting in the siginificant steps for your purposes):
Gets Id of next results to retrieve (through MiniProfiler.Settings.Storage.List())
Retrieves the actual results (MiniProfiler.Settings.Storage.Load(id))
Marks the results as viewed so that they wont be retrieved again (MiniProfiler.Settings.Storage.SetViewed(user, id))
Converts these to ResultsJson and returns it
With access to MiniProfiler.Settings.Storage, you should be able to retrieve, serve and consume the profile results in any way that you want. And if you are interested in using the RenderIncludes engine but want to mess around with the html/js being served, you can provide your own custom ui templates that will replace the default behavior.

Any clientside method to reuse data URI once like a variable?

I've been working on creating my own email client. I'm already successfully displaying attachments and embedded images via data URI. I was wondering once stored in a data if there was any way for me to assign data such as an image to a variable in client-side code (be it XHTML, JavaScript, JSON, etc) so I only have to send a single copy to the client from the server saving bandwidth though be able to display that content (again, such as an image) multiple times?
Pure JavaScript, no frameworks or libraries.
Also the main goal is to transfer the data only once from the client to the server. Once in the DOM it's perfectly okay if we end up having two img elements in example.
As a bonus, I'd like to use two img elements but with the DOM still reference the same single data-uri if possible thus saving memory at the client.
I was wondering once stored in a data if there was any way for me to assign data such as an image to a variable in client-side code
Use one of the following techniques:
Embed the data URI in an SVG element, then dereference it as a background-image URL
Embed the data URI in a script element, then dereference it as a dataset property
Store the data URI in a documentFragment, then wrap it in a function
References
CSS Wikibook:Data-URIs
HTMLElement.dataset
play .wav sound file encoded in base64 with javascript

CakePHP and reusable approach

I would develop my CakePHP application in the most reusable way. I'd like to treat it as webservices, so I don't want to strictly bind controller with view. My idea is: controller just returns json info, the view calls the controller and get the json and make html output.
How can I realize that? Could be a good approch, developing pages rather than views, and inside that pages call the webservices previously developed.
You can even forget about creating view files, using $this->set('_serialize', array('people')); in your PeopleController::show()
Well Cake is kinda' works like this "out of the box". You can use Router::parseExtensions(); to define what type of data you would like to serve. For example in app/Config/routes.php:
Router::parseExtensions('xml','json');
This will make it possible to detect what kind of request is incoming. For example if someone requests:
www.example.com/people/list.json or www.example.com/people/list.xml, in your PeopleController's list() method you'd be able to detect what kind of resource is being requested - json or xml, or of course any other
extension you define. This is what the RequestHandlerComponent is used for. You can check if it is xml for example:
if($this->RequestHandler->isXml()) {
//Some code
}
The different extensions are only different representation of the data, so it shouldn't matter what exactly you're serving. From v2.1 Cake will automatically switch the view class when it sees a JSON or XML request, which takes us to the new JSON and XML views.
All you will have to do is provide the views in the appropriate places.
In View/People (as for this example) you would have:
..View/People/
list.ctp
xml/
list.ctp - XML view
json/
list.ctp - JSON view

use ajax to fetch data from java hashtable in jsp page

I'm working on a project that the web page will fetch the data from java hashtable object in jsp page. I'm using a jsp for gerenated the web page based on HTML. The data is stored in the java hasbtable object on the server. I want to try to make an AJAX call to fetch the data from the server, then display it in the jsp page.
I just want to know if this is possible to do that by make an AJAX call to access the java hashtable object, then fetch the data back to the client side.
Thanks
Here is the test.jsp page which contain the hashtable obejcts:
Hashtable generalTable = (Hashtable) metaDataTable.get("General");
Hashtable adminTable = (Hashtable) metaDataTable.get("Administration");
My inital approach is to make an AJAX call to this test.jsp page. Then try to access those two GeneralTable and adminTable hashtable objects. In those two obejcts, it contains the values I would like to fetch.
Unfortunately, I don't have the code yet for my part because I don't know if this is possible or not.
Yes, it's possible, but you will need to have some server-side code to deal wit the request for the data you want, just like with any other AJAX functionality in any other system.
One way to do this would be to have a "special" jsp that gives you back the data you need without any of the typical HTML. Having the special jsp output it's data as a JSON object will make your life much easier in the client-side code.

Django client side query construction

I'm making a pretty standard AJAXy (well, no XML actually) web page. The browser makes a bunch of API queries that return JSON to run the site. The problem is, I need to add to the API interface each time the page needs to do something new. The new API interface is usually little more than a database query followed by mapping the returned objects to JSON.
What I'd like to do is to get rid of all that server-side duplication and just have the page make database requests itself (using the model interface), but in a way that is safe (i.e. just read only ones). I think this would amount to an interface for constructing Q objects using JSON or something like that, and then send that up to the server, run the query, and return the results. Before I go making my own half-broken architecture for this, I'm wondering if this has already been done well. Also, is this even the best way to go about eliminating this duplication?
Thanks
Search multiple fields of django model without 3rd party app
Django SQL OR via filter() & Q(): Dynamic?
Generate a django queryset based on dict keys
Just replace with operator.and_ where appropriate.

Resources