JavaForAndroid
JavaForAndroid

Reputation: 1179

Calling a URL in jQuery

I would like to call a URL. Currently I am using a hidden iFrame to call this URL:

var data = {File: file,NAME: 'file.txt'};
<iframe src="requestFile?'+ $.param(data) + '" style="display: none;" ></iframe>

Now I would like to use AJAX to call the file. The file just has to be called. I tried it with a GET request:

$.ajax({
    'url' : 'requestFile?'+ decodeURIComponent($.param(data)),
    'type' : 'GET',
    'success' : function(data) {
      if (data == "success") {
        alert('request sent!');
      }
    }
  });

Unfortunately this does not work. May there be a problem with the length of the URL? The file string is quite long. I do not get an error message.

Upvotes: 0

Views: 69

Answers (1)

Mark Schultheiss
Mark Schultheiss

Reputation: 34227

Rather than use the decodeURIComponent($.param(data)), simply use the encode method .param as $.param(data),

decodeURIComponent does just what it says, decodes it and you want to use the encoded which $.param should do for you: http://api.jquery.com/jquery.param/

NOTE: on that page they use the decodeURIComponent in an example so you can see the original decoded value/put it in a variable.

Reworked code (my assumption is that you DO return "success" string and not the actual file here?):

var myparam = var data = {File: file,NAME: 'file.txt'};
$.ajax({
    url: 'requestFile?' + $.param(myparam),
    type: 'GET'
}).done(function (data) {
    if (data == "success") {
        alert('request sent!');
    }
});

NOTE: You did not show how file was defined so I can only make the assumption that it is a JavaScript object somewhere.

Upvotes: 1

Related Questions