Sending HTML Code Through JSON

html in json response
passing html in json object
html link in json
display json data in html page
generate html from json
embed json in html
api json to html
print json in html

I've got a php script which generates HTML content. Is there a way to send back that HTML content through JSON to my webpage from the php script?

Yes, you can use json_encode to take your HTML string and escape it as necessary.

Note that in JSON, the top level item must be an array or object (that's not true anymore), it cannot just be a string. So you'll want to create an object and make the HTML string a property of the object (probably the only one), so the resulting JSON looks something like:

{"html": "<p>I'm the markup</p>"}

Write HTML string in JSON, It is possible to write an HTML string in JSON. You just need to escape your double-quotes. One way is to replace the double quotes in the HTML with single quotes but using double quotes has become the standard convention for attribute values in HTML. Historically, XMLHttpRequest was designed to fetch and send XML as an exchange format, which has since been superceded by JSON. But neither XML nor JSON fit into form data request encoding. But neither XML nor JSON fit into form data request encoding.

Do Like this

1st put all your HTML content to array, then do json_encode

$html_content="<p>hello this is sample text";

echo json_encode($json_array);

Putting HTML in JSON, Sending HTML Code Through JSON - PHP Solutions - Developers Q & A - [] things you should consider beyond what json_encode provides, as  JavaScript Object Notation (JSON). It is a lightweight data transferring format. It is very easy to understand by human as well as machine. It is commonly used to send data from or to server. Nowadays it is widely used in API integration because of its advantages and simplicity.

Just to expand on @T.J. Crowder's answer.

json_encode does well with simple html strings, in my experience however json_encode often becomes confused by, (or it becomes quite difficult to properly escape) longer complex nested html mixed with php. Two options to consider if you are in this position are: encoding/decoding the markup first with something like [base64_encode][1]/ decode (quite a bit of a performance hit), or (and perhaps preferably) be more selective in what you are passing via json, and generate the necessary markup on the client side instead.

JSON HTML, JavaScript can be used to make HTML in your web pages. HTML Table. Make an HTML table with data received as JSON: xmlhttp.send("x=" + dbParam);. HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8 Exercises HTML Exercises CSS Exercises JavaScript Exercises SQL Exercises PHP Exercises Python Exercises jQuery Exercises Bootstrap Exercises Java Exercises C++ Exercises C# Exercises

All string data must be UTF-8 encoded.

$out = array(
   'render' => utf8_encode($renderOutput), 
   'text' => utf8_encode($textOutput)

$out = json_encode($out);

Passing HTML via JSON?, I am trying to pass some HTML code from an ASP page back into a of the html is wrong and it thinks msg is undefned as the json code in  JSON: J ava S cript O bject N otation. JSON is a syntax for storing and exchanging data. JSON is text, written with JavaScript object notation. When exchanging data between a browser and a server, the data can only be text. JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server.


$data = "<html>....";

Then you should use AJAX to retrieve the data and do what you want with it. I suggest using JQuery:

Translating a JSON data to HTML Element, Generally JS template engines accept JSON data and render HTML from an HTML string to insert into the page - here's the result of the code below <button name=string type=button>Table Using HTML String</button>  The JSON array is then sent to the Controller using jQuery AJAX function and once the response is received it is displayed using JavaScript Alert Message Box. //Reference the Name and Country TextBoxes. //Get the reference of the Table's TBODY element. //Add Name cell. //Add Country cell.

Render an entire page in JSON format, Using this technique you will be able to convert entire HTML pages to JSON is sent; In order to differentiate those dynamic JSON pages from standard HTML rendered on -/webapp-json-test.html and insert the webapp code below (do note​  json_encode does well with simple html strings, in my experience however json_encode often becomes confused by, (or it becomes quite difficult to properly escape) longer complex nested html mixed with php.

How to send a JSON object to a server using Javascript , In this example we are going to use AJAX (Asynchronus JavaScript And XML), which is defined below. filter_none. edit close. play_arrow. link brightness_4 code html >. < head >. < title >. JavaScript | Sending JSON data to server. </ title >. JSON (or JavaScript Object Notation) is a lightweight, easy and popular way to exchange data. jQuery is not the only tool for manipulating and interfacing with JSON; it’s just my and many others’ preferred method. A lot of the services we use everyday have JSON-based APIs: Twitter, Facebook and Flickr all send back data in JSON format.

What's actually wrong with an endpoint returning HTML rather than , Related: Is it OK to return HTML from a JSON API? If you send HTML instead and expect the client to extract the data from the HTML, that's rubbish. In a modern application, API code should be responsible for data, and 

  • If you're just sending markup back, why JSON encode at all?
  • because a plugin and it has some limitations...
  • There are some additional things you should consider beyond what json_encode provides, as explained in this post - 4 Things You Must Do When Putting HTML in JSON.
  • You might want a conditional action based on the response i.e if operation successful then show HTML, if failed show appropriate error message
  • @the_archer @T.J. Crowder : I am doing the same thing my array {"success":"true","data":"'.nl2br($a).'","type":"text"} where $a is text posted by user....after inserting user input into database I am sending this json array back to client
  • Whats the security concern sending HTML on JSON?
  • The data needed might be HTML