Url Encode a String in Jquery for an Ajax Request

URL Encode a string in jQuery for an AJAX request

Try encodeURIComponent.

Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

Example:

var encoded = encodeURIComponent(str);

$.ajax() processing my url encoded string

You can use data: {query: requestXML}, to pass the parameters and they get url encoded by jQuery.

The point is the the param gets attached to the url as-is if it already a string.

Quote

data (Object, String)

Data to be sent to the server. It is
converted to a query string, if not
already a string
. It's appended to the
url for GET-requests. See processData
option to prevent this automatic
processing. Object must be Key/Value
pairs. If value is an Array, jQuery
serializes multiple values with same
key based on the value of the
traditional setting (described below).

Since you use GET method, your params get added to the URL string. The decoding then happens from the browser and not from jQuery.

jquery/ajax call to webapi and URL encoding with backslash

You totally should forget about sending special characters in the path portion of an url as Scott Hanselman explains in this blog post: http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx

I will only quote his conclusion that should put you on the right track of what you should be doing:

After ALL this effort to get crazy stuff in the Request Path, it's
worth mentioning that simply keeping the values as a part of the Query
String (remember WAY back at the beginning of this post?) is easier,
cleaner, more flexible, and more secure.

So, encodeURIComponent is great for sending values as query string parameters or POST values which is how usernames and passwords should be sent.

jQuery encode blank space in ajax query string

You need to decode the string before sending it in the request so that it does not get double-encoded by jQuery's $.ajax() method. To do that you can use decodeURIComponent(). Try this:

var station = decodeURIComponent("KDEN%20KSEA");
$.ajax({
type: "GET",
url: 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=tafs&requestType=retrieve&format=xml&hoursBeforeNow=3&timeType=issue',
data: { stationString: station },
success: function(data) {
// do something with the returned data...
}
});

AJAX Post request encoding

to create a query string, you should try using $(this).serialize() instead of serialize array.

$('#create-event form').submit(function(e) {
var postData = $(this).serialize();
var formURL = $(this).attr('action');
$.ajax({
url : formURL,
type: 'POST',
data : postData,
contentType: 'text/plain'
});
e.preventDefault();
});


Related Topics



Leave a reply



Submit