1

I use jQuery to contact my REST service on server side. The URL looks like this:

http://bla.com/?userid=1,2,3,4,5,6...

The userid string could be very long and it could happen that the max url size will be exceeded. I can't do a post request, so my question is, whether it would be a good solution to send the userid data within the header? Something like this:

$.ajax({
    url: 'someurl',
    headers:{'foo':'bar'},
    complete: function() {
        alert(this.headers.foo);
    }
});

Would this be possible? Thanks

Christian
  • 6,961
  • 10
  • 54
  • 82

2 Answers2

1

Yes you can send header using Ajax request

$.ajax({
    type: "GET",
    beforeSend: function(request) {
        request.setRequestHeader("Authority", authorizationToken);
    },
    url: "entities",
    data: "json=" + escape(JSON.stringify(createRequestObject)),
    processData: false,
    success: function(msg) {
        $("#results").append("The result =" + StringifyPretty(msg));
    }
});

you can read more about header on jQuery AJAX page

Also check this example here

Nurdin
  • 23,382
  • 43
  • 130
  • 308
Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102
  • Thanks. Could you explain where i should put my userids in? Here: request.setRequestHeader("Authority", authorizationToken); or here: data: "json=" + escape(JSON.stringify(createRequestObject)) ? – Christian Aug 04 '14 at 07:34
  • It depends on how you are going to process userid on server side, if you want to send it via header put it here like request.setRequestHeader("userid", 10); if on server side you are reading like $_GET['userid'] then add it in data – Subodh Ghulaxe Aug 04 '14 at 07:37
0

This is totally possible, alhough it would be advisable to just use POST, because it was meant to transfer lots of data.

Using headers is a workaround which results in the same results as POST; losing the ability to navigate forward and back without re-submitting (or re-sending headers in this case, will be impossible if the data is gone).

twicejr
  • 1,319
  • 3
  • 13
  • 21
  • Thanks, problem is, we use JSONP. JSONP doesn't seem to work with post requests – Christian Aug 04 '14 at 07:33
  • 1
    Actually it is possible, altough it seems like a rather daunting task: http://stackoverflow.com/questions/5345493/using-put-post-delete-with-jsonp-and-jquery – twicejr Aug 04 '14 at 07:36