Play framework. Action Not Found. GET instead of POST - html

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)

Related

Is it possible to put several URL in a href parameter?

I want to put a link with the <a> tag on my website page, but with multiple URLs in case the 1st one doesn't work. I would be something like this:
<a href="url1;url2;url3"
But of course this doesn't work. So is there a way to do this ?
Thank you
The closest you could come would be to:
Put a default URL in the href
List alternative URLs elsewhere (such as in a data attribute)
Bind a click event handler that runs some JavaScript which prevents the default behaviour and then tests each URL in turn until it finds one that works (or runs out).
Testing would require Ajax which would require either:
Permission via CORS from each URL you test
The user of a proxy (which would test availability of the URL to your proxy and not to the browser)
The additional HTTP requests would consume bandwidth (and thus time).
What do you mean by "1st one doesn't work" ? That the server is sending an error ?
It's not possible to do what you want just with HTML.
It could be done by Javascript by adding a listener to the anchor, sending HTTP request on the first url to see if you receive 200 or an error like 500.
In case of an error, you check the second url and so on and redirect the user where you received a 200.
Otherwise, if you just want to open all the links, there was already a post about that here: How can I open multiple links using a single anchor tag
There's no way of doing this with pure HTML since HTML can't detect whether or not what you mean by "doesn't work" applies to a given URL.

Extract POST URL using GET

I am trying to simulate the functionality of a form in this website, but don't know exactly what the post URL looks like.
The link to the website is here:
selfservice.mypurdue.purdue.edu/prod/bwckschd.p_disp_dyn_sched >> then click on Spring2013. The code I am trying to replicate is the one that happens when the user clicks Course Search and selects CS from the subject list.
You can look at the HTML file to see the values they use in their POST command. How do I see what the values look like once the button is clicked, as I am trying to replicate this and set the variables to the same values. What I need is a URL to be shown with all of the variables set to their respective values. I understand this can be done with a GET command. Can someone tell me how to extract this URL for me so I can proceed?
I edited the page using chrome inspector and changed the form action to GET - this is the URL that was displayed.
https://selfservice.mypurdue.purdue.edu/prod/bwckschd.p_get_crse_unsec?term_in=201320&sel_subj=dummy&sel_day=dummy&sel_schd=dummy&sel_insm=dummy&sel_camp=dummy&sel_levl=dummy&sel_sess=dummy&sel_instr=dummy&sel_ptrm=dummy&sel_attr=dummy&sel_subj=CS&sel_crse=&sel_title=&sel_schd=%25&sel_from_cred=&sel_to_cred=&sel_camp=%25&sel_ptrm=%25&sel_instr=%25&sel_sess=%25&sel_attr=%25&begin_hh=0&begin_mi=0&begin_ap=a&end_hh=0&end_mi=0&end_ap=a
However, this URL dosen't resolve as the script is obviously expecting POST data.

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>

how to reset a dynamically/ajax-ly added/created form using jquery?

In my webpage a form is inserted via Ajax. I am using $("#my-ajax-returned-form")[0].reset();
to reset the form but it doesn't work for me. Firebug console shows TypeError: $("#my-ajax-returned-form")[0] is undefined. Any clue how to fix it? The actual flow is as following and its plain vanilla AJAX with jquery
Click on a icon sends an ajax call to server
The server sends a form which is inserted into webpage using $('#target-div').html(returnedFormHTML) which contains my form with id my-ajax-returned-form
User fills up form and clicks save, data is posted to server using $.post()
On success I fire another method which is trying to reset my-ajax-returned-form using $("#my-ajax-returned-form")[0].reset();
Now, it is obvious that form has to be some live in some sense but I am unable to figure it out :(
Here is the main webpage - http://pastebin.com/rtkr8RUC
Here is the #my-ajax-returned-form form - http://pastebin.com/jtjakgUt
I can not post the complete code on jsfiddle easily, bear with me for that
Update
I found the issue, there is a reset button in the form named reset, so obviously form.reset is not a function.
But the textarea is still not reset!
Fixed the textarea issue as well
The undefined clearly indicates that there's a problem finding the form. Are you sure the selector is correct, are you looking for it inside the correct iframe etc. Try logging the $("#my-ajax-returned-form") to see if it finds anything.
You could try $('#my-ajax-returned-form').eq(0).reset(), but since the selector is an id and so should be unique, this doesn't seem to make much sense.
On the other hand, is the reset() - function even defined?

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

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.