Muhammad Usman
Muhammad Usman

Reputation: 10926

base64 is not encoding in ajax

I want to send base64 string through ajax I encoded it through JavaScript function escape

data =  escape"data:image/png;base64,iVBOR w0K+GgoAAAAN+SUhEUgAAAU oAA+ADmCAYAAAC+zgMwkAAAgAElEQ+VR4Xu1de3BVx3n");

It worksfine and encode it as

data%3Aimage/png%3Bbase64%2CiVBOR%20w0K+GgoAAAAN+SUhEUgAAAU%20oAA+ADmCAYAAAC+zgMwkAAAgAElEQ+VR4Xu1de3BVx3n

But when I send it through ajax

$.ajax({
url: 'http://fiveriverstech.com',
type: 'POST',
data: "data="+data,
success: function(response){
                console.log(response)
                }
            });

It replaces the + sign whit white spaces as data:data:image/png;base64,iVBOR w0K GgoAAAAN SUhEUgAAAU oAA ADmCAYAAAC zgMwkAAAgAElEQ VR4Xu1de3BVx3n

How I can prevent to do this

JS Fiddle

Upvotes: 2

Views: 2793

Answers (1)

lanzz
lanzz

Reputation: 43168

You probably want to use encodeURIComponent() instead of escape(). escape() is not intended to be used for URLs and form data.

Even easier would be to use

data: { data: 'data:image/png;base64,...' }

in your $.ajax() options, without bothering to encode it; jQuery will take care of everything.

Upvotes: 3

Related Questions