开发者

How to encode entire html response data and then decode it via JS?

I would like to encode all data being fetched by an ajax request (so the data has to be encoded on the server side) and then once the data is received by the client it will be decoded via JS and then processed as readable HTML data.开发者_如何学Python

I am using PHP to render the data on the server. What is the most efficient,fastest,easiest way?

Thanks!


You're not trying to reinvent HTTP Request / Response are you? if so, ditch the AJAX and just request a page.

If however you are just trying to load up "some" HTML content to dump into a container on the page then jQuery has a simple method called .load()

http://api.jquery.com/load

Update: based on the discussion in the comments, here is an insecure quick-n-dirty obfuscation technique between PHP on the server and JS on the client using ROT13.

Again I'd like to emphasize that this answer is just about a technique - since there is nothing gained from this process other than additional overhead - Thus I wouldn't recommend using it.

<?php
  function rot13($s){
    return !$s ? "" : strtr($s,
      "NnOoPpQqRrSsTtUuVvWwXxYyZzAaBbCcDdEeFfGgHhIiJjKkLlMm",
      "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz");
  }
  //encode the HTML content
  $encodedStr = rot13($originalHTML);
?>

Then once you've sent it to the browser, decode it in JavaScript:

String.prototype.rot13 = function(){
  return this.replace(/[a-zA-Z]/g, function(c){
    return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
  });
};
//decode the HTML content
 var decodedHTML = ajaxResponseHTML.rot13();


Communicate with PHP through JSON, with json_encode() and json_decode().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜