玩框架 - ajax调用

时间:2016-04-03 15:26:04

标签: javascript ajax playframework

我正面临着制作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)

控制器 Products.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中的第一个警报(“在Ajax中”)弹出,而不是第二个警报(“结束Ajax”),登录控制器(“Products.java”)未记录(在控制台或日志文件中)。我猜ajax电话没有启动,但我不确定我错过了什么。

提前感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:0)

在我看来,你需要使用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()将调用。 我希望这可以帮到你。