Sending HTML through JSON using AJAX - javascript

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

Related

How to get JSON / JSONP data using XAMPP local server?

I have set up a local server using XAMPP. Now I want to get JSON / JSONP data from that server.
Side question:
Do I need to somehow upload the JSON file on the server?
Or is there somePHPcoding for that? IF yes, which?
I have heard about some jQuery ajax call function, but it didnt work for me.
Any ideas?
You have several way of doing that. if the result is not dynamic you just create a json file on server and get it using URL.
if you want to do it dynamic in response to web page sent to server you can use the built in PHP function json_encode.
Please follow php documenation for json_encode
i don t think you need to do a lot of things.
json is a way to format data. you can do that returnin an output using json_encode()
exemples and info at http://www.php.net/manual/en/book.json.php
One method is to use json_encode() function available in PHP to send the data is JSON format to the client and handle the response in AJAX by mentioning dataType: json

send unescaped string with XHR (javascript)

I have string containing javascript file content and I need to upload it to server (it's not part of server side code - it's used only for development (on-site code editing) process so security is not important). PHP script uses file and content variables. I found only implementations where xhr.send() argument was string in standard GET format (var1=sth&var2=sthelse) but I'd like to send this string without any encoding / decoding / escaping / unescaping on server side. As it's js it contains all possible characters including '&' or '='.
Is it possible to pass data to POST in other way than using standard query?
I'm rather not interested in jQ solution
Sending JS file contents in a query string parameter is not a good idea. A few points:
If your situation support HTML5 you can send files to your server with JS.
If you don't want to send as a file upload I would send it as POST data.
Any data encoded or escaped on the client can be decoded or unescaped on the server. In some setups this is automatically done for you. There's no reason to avoid this if it's the proper way to handle data.

How do you receive a file stream in javascript and POST it using AJAX?

I'm receiving a file using a FORM POST in ASP.NET and sending it back like this:
StreamReader sr = new StreamReader(fStream);
Response.Write(sr.ReadToEnd());
What I want to do at this point is take the data and then repost it using AJAX. The problem is I am trying to understand how the data should be encoded or added to the data attribute of a jQuery AJAX POST.
Is this even possible? Can I receive a file stream server-side, send it back as a stream (or something else), store it in a JS variable and then send it as the 'data' in a jQuery AJAX POST?
Yes it is possible to upload a file using jquery AJAX.
Uploadify
This link can be useful for you. It is exactly same example what you want. See the code and let me know if you have any query. I have successfully implemented it in my code.
Advantages :
It is also helpful for you if your file size is too large.
It is easy to implement.
AJAX.

Javascript: How to grab fragment from URL and write it into some PHP?

On the client side, I want to grab the complete URL from the window location, and write it into a simple PHP function, so I can echo the #fragments back to the user in the HTML.
my URL is structured like this:
http://example.com/filename.html#var1=FOO&var2=BAR
notice no ?. The query string delimiter in use must be #, if that impacts your proposed solution.
I want to take that complete url exactly as it appears in window location write it into this simple PHP function::
<?php
parse_str (parse_url ("$_want_to_write_the_full_URL_here", PHP_URL_FRAGMENT ), $args);
echo $args['var1'], ' ', $args['var2'];
?>
I know I can get get window location like this example:
<script>alert(window.location);</script>
But how to get it into the PHP, using javascript on the client side?
thank in advance to all you wizards!
Since server side PHP runs before any client side JavaScript runs, the only way to get the data back to PHP is to make a new HTTP request.
There are several ways to do this including:
The XMLHttpRequest object
Dynamically generating a script element (as per JSONP)
Dynamically generating an iframe/img/etc
Assigning a new URI to location so the browser leaves the current page
Dynamically generating a form and calling its submit() method

How can I send XML data format from browser to server using jQuery?

I would like to know if there is a way in jQuery to send data to server from a browser in XML format?
Thanks,
Sana.
XML is just text so just send it as a string parameter in an AJAX request. Simple as that.

Categories

Resources