I am using php & jquery. I use ajax calls to load some huge html content got from some other php page. As it gets slower as time goes, i understood that the problem is that , I have to return the result as JSON. The HTML content is of a table of some html contents and some events inside it
b开发者_开发问答ut how should i do this. anything spl needed ?
In your PHP page that returns the html content, you can do this:
// $html should contain the html code you want to return, e.g
// <table>...............</table>
$html = json_encode(array('html'=>$html));
echo $html;
die;
In the javascript, you could do something like this:
function getHtml()
{
$.getJSON("yourphpPage.php", function (result)
{
$("#yourHtmlDiv").html(result.html);
}
);
}
You should first try to understand what json is. It is just a format for hierarchial information. Json by itself gives no meaning to that information, unlike HTML. For example, there are no such things as "events", unless you manually process them and run the event handlers.
However, if you can get your information to be in json format and still be useful, it will most likely take less space/bandwith, will load and even parse faster than html.
Reading and parsing json is quite easy, since it based off JavaScript. Any JSON code is also a valid JavaScript, so you could parse it for example with eval. However you should do that only if you completely trust the source of json as not being malicious, because running anything you get from a request in an eval() is not very safe. If you don't complete trust the input, you could use for example http://api.jquery.com/jQuery.getJSON/
if i understand your problem, u want the return data in jSON then simple use
jQuery.ajax({
dataType:'json'
})
精彩评论