10

私は多くの jQuery ajax チュートリアルをふるいにかけ、それを自分の Play に取り入れようとしました! アプリですが、いくつかのことをよく理解していません。Ajax呼び出しを介して次のことを行う方法を誰かが説明できる可能性はありますか:

1)コントローラーから連絡先のリストを取得したいとします(各連絡先には名前、電話、メールがあります)。コントローラーは、テンプレートの適切な応答を「構築」する必要がありますか? コントローラはどのように見えますか? それを取得するためのJavaScriptはどのように見えますか?

2) ajax 呼び出しを介して新しい連絡先を追加/更新する場合、javascript はどのように見えますか?

上記の説明の例のコードを次に示します (ajax を使用していません)。


コントローラ:

public static void list() {
    連絡先をリストする= Contact.fetchAll();
    レンダリング (連絡先);
}

public static void add(String name, String phone, String email) {
    連絡先 contact = new Contact();
    contact.name = 名前;
    contact.phone = 電話;
    contact.email = メール;
    contact.save();
}

public static void update(Long id, String name, String phone, String email) {
    連絡先 contact = Contact.findById(id);
    contact.name = 名前;
    contact.phone = 電話;
    contact.email = メール;
    contact.save();
}


テンプレート (すべての連絡先を一覧表示):

#{連絡先のリスト、as:'contact'}

    ${contact.name}
    ${contact.phone}
    ${contact.email}

#{/リスト}


テンプレート (連絡先を追加):

#{form @Contacts.add(), id:'form'}
<input type="text" name="name" />
<input type="text" name="phone" />
<input type="text" name="email" />
<input type="submit" value="Add" />
#{/形}
4

3 に答える 3

13

私は Play 側に精通していませんが、Ajax 呼び出しを介して JSON データを取得したい場合、コントローラーは次のようになります。

public static void getContacts() {
    List<Contact> contacts = // get contacts here...
    renderJSON(contacts);
}

JSON データを取得するための jQuery は次のようになります。

// the getJSON function automatically parses the returned JSON
// data into a JS data structure
$("#retrieve_contacts").click(function(event) {
    $.getJSON("/url/which/calls/controller/", function(contacts) {
        // do something with contacts list here...
    });
});

連絡先を追加/更新するには、次のようにします。

// call the controller to add the relevant contact with
// the info in the second param:
$("#add").click(function(event) {
    var newcontact = {
        name: $("input[name='name'").val(),
        phone: $("input[name='phone'").val(),
        email: $("input[name='email'").val(),
    };

    $.post("/url/which/adds/new/contact/", newcontact, function(data) {
        alert("Contact added!");
    });
});

多くのエラー処理を追加したいのは明らかです。$.getJSONおよび$.post関数は、より柔軟な$.ajaxのショートカットです。他のオプションについては、それを調べてください。

于 2010-11-24T21:17:08.487 に答える
1

ここでは、scala を使用して json で ajax を使用する簡単な例を示します。

ここで ajax を使用した json のコード

@(list: List[User])(implicit session:play.api.mvc.Session)


@main(""){

     @if(list.size>0){
        @for(i<-list){
            <h1> welcome on ur Profile page </h1>
    your id is             @i.id <br>
    your first name is     @i.fnm <br>
    Your Last Name Is      @i.lnm <br>      
    Your password is       @i.pwd <br>
    And your address is    @i.res <br>
    and ur contact no. is  @i.num <br>      
        }       
    }else{
    Sorry, Please insert data in list before watching this page
    }
    }
<h4> want to know the details of other user click here  </h4><br>
<input type="button" value="FriendRequestList" onclick="friendList()">
<br/>
<br/>
<script>

function friendList() {
    $.ajax({
        type : "POST",
        url : "/userDetail",
        //data : "sender_id=" + id,
        success : function(data) {
            var d=JSON.parse(data);
            var inn="";
            for(var i in d){
            inn+="<label>"+d[i]["fnm"]+"</label>";
            inn+="<input type='button' value='SendRequest' onClick ='sendRequest(\""+d[i]["id"]+"\")'>";
            inn+="<br/>";
            }
            document.getElementById("output").innerHTML = inn;
        }
    });
}

function sendRequest(id) {
    $.ajax({
        type : "POST",
        url : "/sendRequest",
        data:{"receiver_id":id},
        success:function(){
            alert("hi");}
    });

} 
</script> 

<input type="button" value="YourRequest" onclick="RequestList()">
<br/>
<br/>
<script>
function RequestList() {
    $.ajax({
        type : "POST",
        url : "/requestList",
        //data : "sender_id=" + id,
        success : function(data) {
            var d=JSON.parse(data);
            alert(d[0])
            var inn="";
            for(var i in d){

            inn+="<label>"+d[i]+"</label>";
            inn+="<input type='button' value='AcceptRequest' onClick ='AcceptRequest(\""+d[i]["id"]+"\")'>";
            inn+="<br/>";
            }
            document.getElementById("output").innerHTML = inn;
        }
    });
}
function AcceptRequest(id) {
    $.ajax({
        type : "POST",
        url : "/acceptRequest",
        data:{"friend_id":id},
        success:function(){
            alert("request accept succcessfully ");}
    });
}
    </script>
<div id="output"></div>

For Logout Click Here <a href="/logout" >Logout</a> 
于 2014-02-16T07:28:41.390 に答える
0

play をダウンロードして、彼らの予約例を見てください。これは、あなたが探しているものとほとんど同じように見え、jsaction の使用の素晴らしい例です....(さらに、自分で実行することもできます)。

http://www.playframework.org/documentation/1.2.3/tags#jsアクション

基本的に、彼らはhtmlファイルを持っているように見え、返されたhtmlをページのdivに挿入するだけで、ターゲットのhtmlページではそのdivは空で、再生から別のhtmlファイルで埋めます。(すべて予約例にあります)。

于 2012-02-04T17:50:22.230 に答える