Linking to a page with a specific HTTP Method (DELETE) - html

How can I link to a page and make the browser call it with the DELETE method, as Rails does?
I tried DELETE MEbut doesn`t work.
I use Node.js, so I can use it to handle DELETE method.

You can't. Links will only ever trigger a GET request.
You can choose between a GET and a POST in a form.
Other HTTP request types can be made using JavaScript and XMLHttpRequest, but not reliably cross-browser.

You can use a javascript plugin like RestfulizerJs for converting your links to pseudo forms.

Related

Play framework. Action Not Found. GET instead of POST

I'm trying to make POST request, but every time I get this error.
error
I do request from a simple hyperlink in my html-file:
Delete
And I can't use any forms, because I call this inside another form (forms nesting not allowed as I know):
#helper.form(helper.CSRF(routes.UserController.submitBoxes)) {...
Are there any solutions apart from using GET instead of POST?
Click on link with href attribute always work as GET, if you not prevent this event with js. But in you case, you can just add duplicate line in you "routes" file with replace POST to GET. Like this:
GET /users/$id<[^/]+>/delete controllers.UserController.deleteUser(id: ObjectId)

How to specify DELETE method in a link or form?

Rfc2616 lists many methods besides GET and POST, like, say, DELETE, PUT etc. Method field in html forms, though, seems to be allowed to specify only GET or POST.
Is it possible to create a link or form in a html page that uses a request method that is not GET or POST?
Was trying to figure this out for a rails app that was using Angular on the front end; these seems to work for that environment:
<a data-confirm="Are you sure?" data-method="delete" href="/link-to-resource" rel="nofollow">Delete</a>
Edit: Just to give everyone a heads up, I think you still need to have jQuery for this to work. I removed jQuery and it stopped working; I put it back and it started working.
You certainly can’t create a link that uses anything other than GET. Since HTML began, links have been meant to be idempotent and free from side effects.
For forms and XMLHTTPRequests, Caps’ link is the place to look: Are the PUT, DELETE, HEAD, etc methods available in most web browsers?.
By default, not there is no way to do this. Links always perform GETs, forms can use GETs or POSTs.
That said, with a little JavaScript, it's possible. Rails for instance ships with helpers which will add a data-method attribute to links. Rails-UJS is a jQuery library that will transparently intercept clicks on these links, and trigger a form submit with a _method parameter used for overriding the normal HTTP method. Finally Rack will intercept requests with a _method params, and overwrite the request method with the value in _method.
Other frameworks no doubt follow a similar pattern.
If you want even more details, I've written up an explanation of how Rails, Rails-UJS, and Rack all work together to provide this.
It's good to know how your libraries work.
It is not possible to create a link or form with delete method.
Many web framework create a hidden input called "_method" for handling PUT and DELETE.
I created a plugin for automatically convert links to forms : RestfulizerJs
You can take a look here : https://github.com/Ifnot/RestfulizerJs
#Ifnot plugin is great but I created a one based on $.ajax function instead of appending hidden forms! here's a simple example for a DELETE request
HTML
<button class="delete" data-target="http://example.com/post/post-id/" data-method="DELETE" data-disabled="true">Delete Article</button>
JavaScript
$(".delete").restintag(optionsObj, function(data) {
console.log(data);
},
function(error) {
console.log(error);
});
https://github.com/KhaledElAnsari/RESTInTag/
HTMX is designed to solve this.
Why should only <a> and <form> be able to make HTTP requests?
Why should only click & submit events trigger them? Why
should only GET & POST methods be available? Why should you only be
able to replace the entire screen? By removing these arbitrary
constraints, htmx completes HTML as a hypertext
It enables any DOM element to make HTTP requests GET, POST, PUT, PATCH, and DELETE, without writing any custom JavaScript.
just add a data-method attribute,<a data-method='put' href='whatever'>link</a>

AJAX Submit via Post entire Form

I am trying to simply post an entire form w/o the need to create the url like you would have to in a get call. All of the tutorials I have seen for this for some reason create a parameter URL and send it via the send ability.
I want to be able to send a form via the form id or form name, is this possible?
The reason is because I will have some submits that can have anywhere from 2 to 1000 checkboxes the user can press (not my choice).
Example I looked at mainly is: http://www.captain.at/howto-ajax-form-post-request.php
I use a type="button" to do the submit not a onchange or anything like that.
Use jQuery, and it's very easy:
$.post("/myactionpage.php",$("#formID").serialize(), function (data) { whatever(); });
If you can use jQuery, the JQuery Form plugin does exactly that.
The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX. The main methods, ajaxForm and ajaxSubmit, gather information from the form element to determine how to manage the submit process.
There are multiple ways to do this and you're right, you don't want to use the GET method. You want to use the POST method, which allows you to send all the data in the form. There are too many options to list, but jQuery is a good start. If you don't want to use an external library like jQuery, try checking out a google search on "ajax via post".

HTML form method with nice URL

I just want to know whether there is a way to answer this question with "Yes" without using JavaScript.
What I want to do is have a search form that automatically generates URLs like http://example.com/search/my+search+term or something similar when I enter my search term into a search text field.
EDIT: Due to some mis-understanding (and not being clear on my part), a clarification: I want the browser to generate that URL based on the value of the text field when the form is submitted.
No, it's not possible without using JavaScript.
The best you can do is using a GET action and have an url like http://example.com/search/?q=my+search+term, where q is the name of the input search box.
Using html only, no.
You could have something server side that might work. You could have the server respond with a 302 response code. If you are using Apache, you could probably use mod_rewrite to take the GET request and generate a new url.
For example, the browser might ask for http://example.com/search/?q=blah+foo+bar, the server could then take that and send the browser a 302 redirect for http://example.com/search/blah+foo+bar.
See more information at the Apache url rewriting guide, or by using your favorite search engine.
You could still use javascript to generate the correct url, but if someone has javascript disabled, this would work as a fallback.
The answer is No
No if you want it to be client side, if you can do it server side (by submitting the form) you can use something like PHP
Yes you could perform something like this server-side pretty easily as long as you don't mind submitting a form.
EDIT: Upon further clarification from the author in comments below: It is not possible in a pure client-side manner without JavaScript or some other client-side tool like Flash/Silverlight (which is admittedly overkill).

How can I post data (form) to html page and hijacking the data in the middle?

the site addres: http://www.ynet.co.il/YediothPortal/Ext/TalkBack/CdaTalkBack/1,2497,L-3650194-0-68-544-0--,00.html
fill the form with rubbish.
Hit 'Send'
the form post the data to another HTML without any parsing of the data i've just added
How do they do it?
A likely option is that they are using a content management system where "html" on the URL doesn't actually mean it's a static html file.
This may be out of left field, but I've certainly used the occasional JS function to grab everything in the header and either parse it or pass it to another script using AJAX.
I'll sometimes use this method in a 404.html page to grab the headers of the previous page, parse them out to see where someone was trying to go and redirect them.
That is, as annakata said, one of the numerous options available.
Edit based on clarified question:
Numerous frameworks can be configured to intercept an html request - for instance asp.net can be set to handle any given extension and an HTTPModule could do anything with that. It's really up to web server configuration what it decides to do with any request.
also: you don't really want to be saying "hijack"