Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3484122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:45:10+00:00 2026-05-18T10:45:10+00:00

I’ve been sifting through many jQuery ajax tutorials and attempting to incorporate it with

  • 0

I’ve been sifting through many jQuery ajax tutorials and attempting to incorporate it with my Play! app but I’m not quite understanding a few things. Is it possible someone could explain how to do the following through Ajax calls:

1) Suppose I want to retrieve a list of Contacts from a controller (each Contact has name, phone, email). Does the controller need to “build” the proper response for the template? What does the controller look like? What does the javascript look like to retrieve it?

2) For adding/updating a new Contact through an ajax call, what does the javascript look like?

Here is code for an example of the explanation above (not using ajax):


Controller:

public static void list() {
    List contacts= Contact.fetchAll();
    render(contacts);
}

public static void add(String name, String phone, String email) {
    Contact contact = new Contact();
    contact.name = name;
    contact.phone = phone;
    contact.email = email;
    contact.save();
}

public static void update(Long id, String name, String phone, String email) {
    Contact contact = Contact.findById(id);
    contact.name = name;
    contact.phone = phone;
    contact.email = email;
    contact.save();
}

Template (lists all contacts):

#{list contacts, as:'contact'}

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

#{/list}

Template (add contact):

#{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" />
#{/form}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:45:11+00:00Added an answer on May 18, 2026 at 10:45 am

    I’m not familiar with the Play side of things but if you wanted to retrieve some JSON data via an Ajax call the controller might look something like:

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

    The jQuery to retrieve the JSON data would look something like:

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

    To add/update a contact you might do something like:

    // 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!");
        });
    });
    

    You’d obviously want to add in lots of error handling. The $.getJSON and $.post functions are shortcuts for the more flexible $.ajax. Look that up for more options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm looking for suggestions for debugging... If you view this site in Firefox or
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.