0

Is there a way to see if a client browser supports PUT or SEARCH methods for usage with JQuery & AJAX requests?

I have the following code, and PUT does not appear on the server side for me in Chromium and Chrome ... I'd like to know, if PUT isn't supported by the browser, how to change it to a POST request ... for backwards compatibility

function do_data(url, action, query) {
try {
    if ($.browser.msie) {
        var xdr = new XDomainRequest();
        if (query !== null) {
            console.log(query);
            xdr.open(action, url + '?' + $.param(query));
        } else {
            xdr.open(action, url);
        }
        xdr.onload = function() {
            var data = $.parseJSON(this.responseText);
            show_data(data);
        };
        xdr.send();
    } else {
        if (query !== null) {
            $.ajax({
                url: url,
                data: query,
                type: action,
                success: function(data) {
                    console.log(data);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log(textStatus);
                }
            });
        } else {
            console.log(query);
            $.ajax({
                url: url,
                type: action,
                success: function(msg) {
                    console.log(data);
                }
            });
        }
    }
} catch (e) {}
}

Using the above code, if I use "PUT" on Chromium / Chrome, error: function(jqXHR, textStatus, errorThrown) will print out simply error.

On the server side, I see the REQUEST_METHOD: OPTIONS and not PUT.

Just to confirm, for anyone who comes across this ... there isn't a programmatic way

Community
  • 1
  • 1
sdolgy
  • 6,963
  • 3
  • 41
  • 61
  • You could always set up a simple "HTTP echo" service, that would respond to every request with a 200 OK returning the full request (headers and all) in the body. – Szocske Jul 09 '11 at 11:52

1 Answers1

0

The common way of handling the lack of PUT and DELETE support in most browsers is to use HTTP POST tunneling. Basically you use a POST and add the real VERB to a X-HTTP-Method-Override HTTP header. On the service you check for the latter, if not found use the normal HTTP method.

See here for more info.

Maurice
  • 27,582
  • 5
  • 49
  • 62
  • but this doesn't allow me a programmatic way to see if the client browser supports PUT or doesn't .... – sdolgy Jul 08 '11 at 20:34
  • Using this technique you don't have to. Afaik there is no way to check support for PUT and DELETE and even if you could you still need to provision for it this way. – Maurice Jul 08 '11 at 20:46
  • thanks ... that got me on to this: http://stackoverflow.com/questions/1813156/x-http-method-override-in-jquery – sdolgy Jul 09 '11 at 06:11
  • 1
    The other side of the problem is the server bits. Not all recognize the **X-HTTP-Method-Override** header and some, like WCF Data Services, use a different header like **X-HTTP-Method** – Maurice Jul 09 '11 at 06:15