[英]How to get data from MySQL and store it to Java program using PHP script and JSON
[英]Can't get Json data to display using coffee script in Play2.4 based web app with Mysql/Java
我正在使用play 2.4框架來創建一個非常基本的Web應用程序。 這是我的代碼:
Application.java
package controllers;
import play.*;
import play.db.ebean.Transactional;
import play.mvc.*;
import views.html.*;
import models.Players;
import play.data.Form;
import java.util.List;
import com.avaje.ebean.Model;
import static play.libs.Json.toJson;
public class Application extends Controller {
@Transactional
public Result index() {
return ok(index.render());
}
@Transactional
public Result addPlayer() {
Players player = Form.form(Players.class).bindFromRequest().get();
player.save();
return redirect(routes.Application.index());
}
@Transactional
public Result getPlayer() {
List<Players> players = Players.FIND.findList();
return ok(toJson(players));
}}
Players.java
package models;
import javax.persistence.*;
import com.avaje.ebean.Model;
@Entity
@Table(name = "player_info")
public class Players extends Model {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int id;
public String name;
public static Model.Finder<Integer, Players> FIND = new Model.Finder<>(Players.class);
}
Index.scala.html
@main("Welcome to TSI") {
<div>
<script type='text/javascrips' src='@routes.Assets.versioned("javascripts/main.js")'></script>
<ul id="players_list" ></ul>
</div>
<form action="@routes.Application.addPlayer()" method="post">
<input type="text" name="name" />
<button type="submit">Add player</button>
</form>
}
Routes
# Home page
GET / controllers.Application.index()
POST /player controllers.Application.addPlayer()
GET /players controllers.Application.getPlayer()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
Main.coffee
$ ->
$.get "/players", (players) ->
$.each players, (index, player) ->
$('#players_list').append $("<li>").text player.name
當用戶從Web表單輸入時,可以完美地從DB保存/檢索數據。 但是,當將數據從JSON獲取到HTML時,它什么也不顯示(空列表)。
我確實試圖使用JS和其他咖啡腳本,但沒有一個起作用,所以不確定我要去哪里。
任何幫助將不勝感激。 謝謝
看來您有錯字。 代替
<script type='text/javascrips' ...
它應該是
<script type='text/javascript' ...
通過在腳本標簽中使用defer = "defer"
參數解決了問題,因為它無法識別列表標簽,因此必須等待聲明它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.