gpt4 book ai didi

javascript - play框架-ajax调用

转载 作者:行者123 更新时间:2023-11-28 06:11:06 25 4
gpt4 key购买 nike

我在进行 ajax 调用时遇到问题。 Javascript 方法 del 被调用,但没有发生 ajax 调用。这是我的代码:

list.scala.html

@(products: List[Product])
@main("Products catalogue") {
<h2>All products</h2>
<script>
function del(urlToDelete) {
alert("In Ajax");
$.ajax({
url: urlToDelete,
type: 'DELETE',
success: function(results) {
// Refresh the page
//location.reload();
},
error : function(results) {
alert('Make call failed');
}
});
alert("End Ajax");
}
</script>
<table class="table table-striped">
<thead>
<tr>
<th>EAN</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@for(product <- products) {
<tr>
<td><a href="@routes.Products.details(product.ean)">
@product.ean
</a></td>
<td><a href="@routes.Products.details(product.ean)">
@product.name</a></td>
<td>
<ahref="@routes.Products.details(product.ean)">@product.name</i></a>
<button onclick="del('@routes.Products.delete(product.ean)')" >del</button>
</td>
</tr>
}
</tbody>
</table>
}

路线

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# An example controller showing a sample home page
GET / controllers.HomeController.index
# An example controller showing how to use dependency injection
GET /count controllers.CountController.count
# An example controller showing how to write asynchronous code
GET /message controllers.AsyncController.message

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)

GET /hello controllers.HomeController.hello(name: String)

GET /products controllers.Products.list()
GET /products/new controllers.Products.newProduct()
GET /products/:ean controllers.Products.details(ean: String)
POST /products controllers.Products.save()

DELETE /products/:ean controllers.Products.delete(ean: String)

Controller 产品.java

public class Products extends Controller {
...
public Result delete(String ean) {
logger.info("delete product");
final Product product = Product.findByEan(ean);
if(product == null) {
return notFound(String.format("Product %s does not exists.", ean));
}
Product.remove(product);
return redirect(routes.Products.list());
}
...
}

我没有收到任何错误,我的浏览器也没有显示任何错误。 javascript 中的第一个警报(“In Ajax”)弹出,而不是第二个警报(“End Ajax”),登录 Controller (“Products.java”)未记录(在控制台或日志文件中)。我猜 ajax 调用没有启动,但我不确定我错过了什么。

预先感谢您的时间和帮助。

最佳答案

在我看来,您需要使用 delete() 方法来映射 ajax 调用。像那样在您的 Ajax 中

 $.ajax({
url: /urlToDelete,
type: 'DELETE',
success: function(results) {
// Refresh the page
//location.reload();
},
error : function(results) {
alert('Make call failed');
}
});

以及您的路线

# Routes
GET /urlToDelete controllers.Products.delete()

因此将调用方法delete()。我希望这可以帮助你。

关于javascript - play框架-ajax调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36387319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com