sending an HTTP request with an unordinary method

277 views Asked by At

I am trying to create a link on my web page that when accessed will cause an HTTP request with an unordinary method to be sent (for the sake of the example, the method is POSTS). I know that regular HTML forms support only GET and POST and AJAX should support PUT and delete. Is there a way to issue requests with a method different than these standard HTTP methods? Thanks in advance.

==============

After the tips I adjusted my page to have the following HTML:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.ajax({url:"http://www.example.php",type:"POSTS",dataType:"html"});
  });
});
</script>
</head>
<body>
<button>Send Request</button>
</body>
</html>

Surprisingly, this did not work in Chrome and Firefox but did work in an updated version of IE: When I tried to issue the request using Firefox and Chrome, the browser issued a request with an OPTIONS method and the following header:

Access-Control-Request-Method: POSTS

Only IE issued the request as per the requirement - using the POSTS HTTP method.

===

Turns out that the aforementioned HTML doesn't work.(The desired request with the custom header is not issued). As I mentioned above, firefox and chrome issue an initial request with an Access-Control-Request-Method header , while IE doesn't issue a request at all. I adjusted the browser security settings (enabled "Access data sources across domains), but still, the browser did not issue the request. I'm guessing there needs to be some further adjustments in the browser setting, does anyone have an idea how this can be solved?

1

There are 1 answers

2
Tom On

Quick answer yes if you use javascript.

Methods are defined by the standard along with status codes however they are open to extension. Such as webdav which increases the method vocabulary.

Not all browsers support arbitrary http methods especially older versions of IE (surprise surprise!)

So you could simply have a link / button which calls a javascript function which makes your http request.

Simply using jQuery:

var request = $.ajax({
  url: "script.php",
  type: "POSTS",
  data: { id : menuId },
  dataType: "html"
});

request.done(function( msg ) {
  $( "#log" ).html( msg );
});

request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});