loading html content as a json

后端 未结 3 801
死守一世寂寞
死守一世寂寞 2021-01-16 10:41

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 t

相关标签:
3条回答
  • 2021-01-16 11:06

    if i understand your problem, u want the return data in jSON then simple use

    jQuery.ajax({
    
                  dataType:'json'
    })
    
    0 讨论(0)
  • 2021-01-16 11:17

    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/

    0 讨论(0)
  • 2021-01-16 11:19

    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);
           }
         );
    }
    
    0 讨论(0)
提交回复
热议问题