2016-04-03 17 views
0

Ich stehe vor dem Problem, einen Ajax-Aufruf zu machen. Die Javascript-Methode del wird aufgerufen, aber es findet kein Ajax-Aufruf statt. Hier ist mein Code:play framework - ajax call

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> 
} 

Routen

# 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 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()); 
} 
... 
} 

Ich erhalte keinen Fehler und mein Browser zeigt keinen Fehler an. Erste Warnung ("In Ajax") in JavaScript wird eingeblendet, nicht die zweite Warnung ("End Ajax"), Login-Controller ("Products.java") wird nicht protokolliert (in der Konsole oder in der Protokolldatei). Ich denke, Ajax-Anruf wird nicht initiiert, aber ich bin mir nicht sicher, was ich vermisse.

Vielen Dank im Voraus für Ihre Zeit und Hilfe.

+0

Veröffentlichen Sie den entsprechenden Eintrag aus der 'routes' Datei – Anton

+0

Zeigen Sie Ihre' routes' –

+0

Erhalten Sie eine Fehlermeldung msg? Was passiert in Ihrem Browser in Ihren Entwicklertools? – Kris

Antwort

0

Meiner Meinung nach müssen Sie Ihren Ajax-Aufruf mit Ihrer delete() -Methode zuordnen. Wie die Zu Ihrem Ajax

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

Und in Ihren Routen

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

So Methode delete() werden Aufruf sein. Ich hoffe, das kann dir helfen.