简体   繁体   中英

Sending HTML through JSON using AJAX

I'm working with Mercado Livre API.

Since the user is allowed to use HTML in description field, I must allow it too. I'm using AJAX to communicate with Mercado Livre, but when I'm trying to parse a big HTML code, I'm not able to use json_decode on PHP.

How can I encode the user's HTML before sending it to PHP and decode it when it's received?

You first, require encode all HTML and next send this to API, for this, you can use the next function:

var encodedHtml = originalHtml.replace(/[\u00A0-\u9999<>\&]/gim, function(idx) {
  return '&#'+idx.charCodeAt(0)+';';
});

This code is on side client. To decode HTML in your server side, you need the use html_entity_decode PHP sentence.

I hope this help you.

Greetings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM