Reputation: 5280
I'm in the process of adding some AJAX functionality to my Play application, following instructions provided by steps 5 (Adding some AJAX actions) and 6 (Invoking actions from Javascript) of the Zentask tutorial for Play Java.
My current setup for triggering a specific action when a <button>
is clicked is as follows:
PUT /resources/:name/description controllers.Application.updateDescription(name: String)
...
GET /assets/js/routes controllers.Application.javascriptRoutes()
...
public class Application extends Controller {
public static Result javascriptRoutes() {
response().setContentType("text/javascript");
return ok(
Routes.javascriptRouter(
"jsRoutes",
controllers.routes.javascript.Application.updateDescription()));
}
...
public static Promise<Result> updateDescription(String name) { ... }
}
<script src="@routes.Application.javascriptRoutes()" type="text/javascript"></script>
...
...
button.on("click", function(event) {
event.preventDefault();
resourceName = "foo";
route = jsRoutes.controllers.Application.updateDescription(resourceName);
newDescription = "bar";
$.ajax({
url: route.url,
type: route.type,
data: { "description": newDescription },
success: function(res) {
alert("Success!");
},
error: function(err) {
alert(err);
}
});
});
While this works (I get a "Success" alert every time I click the button), looking at console output in Firefox developer tools I noticed that after the PUT
request is executed, Play seems to be executing a separate GET
request to the same URL: The console displays a line saying "no element found", and when I click on the corresponding link, a new window containing the source of a regular "Action not found" page opens. This page (correctly) states that no corresponding action could be found "For request GET /resources/foo/description
" and lists all routes I have currently defined. This behavior persists when I change the HTTP method from PUT
to POST
.
My question is: Why is Play complaining about a missing action that I am never triggering? How can I keep it from checking for this action?
Upvotes: 0
Views: 1190
Reputation: 571
Do you get the same console output in Chrome? Could be firefox prefetching URLS.
Upvotes: 1