Ajax – JSON doesnt get sent in PATCH only

First, check that you use latest version of jQuery library:

  • Older versions directly restrict unknown methods (PATCH is new one).
  • I’ve tested on jQuery 1.7 – PATCH method working without problems.

Second, not all browsers supports PATCH method using XMLHttpRequest:

  • Like, IE 7,8 (9+ works okay) have XMLHttpRequest, but it throws an error on PATCH:

    new XMLHttpRequest().open('PATCH', "https://stackoverflow.com/"); //Illegal argument
    
  • To fix this, you may force jQuery to use the old proprietary ActiveXObject xhr, like so:

    $.ajax({
        url : 'http://127.0.0.1:8001/api/v1/pulse/7/',
        data : data,
        type : 'PATCH',
        contentType : 'application/json',
        xhr: function() {
            return window.XMLHttpRequest == null || new window.XMLHttpRequest().addEventListener == null 
                ? new window.ActiveXObject("Microsoft.XMLHTTP")
                : $.ajaxSettings.xhr();
        }
    });          
    

Leave a Comment