Christian
Christian

Reputation: 7419

Sending data to server using get request header

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

Upvotes: 1

Views: 3081

Answers (2)

Subodh Ghulaxe
Subodh Ghulaxe

Reputation: 18651

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

Upvotes: 1

twicejr
twicejr

Reputation: 1329

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).

Upvotes: 0

Related Questions