Embed already-formatted HTML into JSON - javascript

Is there a way to embed HTML into JSON?
I have formatted HTML data in my database. I am querying this database and serializing the response to JSON. However, when I incorporate this JSON into my web page, its not what I expect!
SQL returned data:
<b>Trolo:</b><br/><b><a target="_blank" class="trololo" href="http://trololooo">TROLOLOLOOO</a></b>
JSON returned data:
"TROLO":"\u003cb\u003eTrolo:\u003c/b\u003e\u003cbr/\u003e\u003cb\u003e\u003ca target=\"_blank\" class=\"trololo\" href=\"http://trololooo\"
Should I be doing some kind of encoding of the HTML data when it is serialized into JSON?

Use URL-encoding and URL-decoding to encode and decode your HTML.
This might also be helpful: https://stackoverflow.com/a/4292961/1182823
In your scenario, it seems you'll have to do the encoding on your server-side (e.g. with PHP before saving data in the database).

Related

how to convert part of XML to string and store it in a variable using Javascript

I have a below scenario:
During run time I want to take whole request and place in response
Request is in XML format
I want to take that request and store it in a variable either it is in string or XML also fine
4.It should be done with Java script
I am able to take the request and convert it to JSON with JSON.Stringify function. In the same way is there any function to read directly as XML or converting XML to String and store it in a variable?

Feeding data into database with Php form and using that data into JSON format to be used in D3

All I want to do is to get some data from JSON file for my D3.js in real time that too from user directly and get updated instantly.
There will be a php form from which data will be stored into database and the data from the database will be converted into JSON file which will be used by d3.js for some visualization technique.
Problem I am facing
I am not sure whether I can get the data from php form and convert it to JSON file instantly and can use for my Visualization model. I can convert the data into into database into JSON file but not sure how to do it one go.
Why you want a json file if your data is saved on database? Just json_encode($array_of_your_data_from_database) will give you a json respond easily.

Send and receive a blob in javascript

I want to send a blob using a JQuery ajax request and receive it server-side with Node.js + express.
I would send the blob as a JSON string, but it seems that none of the binary data is included in it:
{"type":"audio/wav","size":344108}
How else could it be sent?
JSON doesn't support binary data. You will have to encode your binary data first: Binary Data in JSON String. Something better than Base64

Jaggery JS Convert WSRequest to JSON

I am calling a data service from a jaggery js app using http://jaggeryjs.org/apidocs/ws.jag . The data comes back in XML with members .responseText and .responseE4X. Is there an easy way to convert this response to JSON? Is there a parameter I can set to have this respond with JSON? This is being run in the server side, so I do not have access to window(), which most XML to JSON converters use.
Thanks!
I did this by using the post method: http://jaggeryjs.org/apidocs/post.jag
It is worth noting that there is a bug in this as well. I had to set the content type header as "content-type" and not "Content-Type".

Posting a file using Json

I want to upload a binary file using json.
I choose Json because with the file I would also like to send additional information.
I am going to do this by -
Select a file in the file input tag.
Use the HTML5 File Reader Api to read a file first.
Convert the file content into base64.
Add the base64 content to a JS object in a data uri format.
Convert the JS object to json and post it to the server.
I wonder if this is the only legitimate way to achieve my goal? Also, if there is a plugin already available somewhere which give me this ability?
No, this is not the only way - one of the other ways is just to submit a form with a file in it. Such form uses multipart/form-data content type.
See W3C documentation on the subject:
The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters.
The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
So, there is no need to reinvent the wheel - browsers already support sending the files along with additional information, in a simple way. You just create a form where the user can enter data and select files, then all of them are sent to the server with multipart/form-data content type, and your web framework should be able to understand that it deals with both files and textual data.

Categories