Announcement Announcement Module
Collapse
No announcement yet.
ajax with grails Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ajax with grails

    Hello!

    I wanted to do some Ajax with Grails. How can I change the below code in order to directly read out some information from the xml:

    Code:
    def listXml ={
            def books = Book.list(params)
            render(contentType:"text/xml"){
                booklist{
                    for(b in books){
                        book(id:b.id){
                            title(b.title)
                            author(b.author)
                            
                        }
                    }
                }
            }
        }
        
        def showBooks = {
            render(template:'results', model: [value: params.value, books: books])
        }
    
    search.gsp
    <html>
      <head>
        <meta name="layout" content="main" />
        <title>Books Find</title>
      <g:javascript library="scriptaculous" />
    </head>
    <body>
      <h2>Ajax Search</h2>
    <g:formRemote name="showBooks"
                  url="[controller:'book',action:'showBooks']"
                  update="[success:'results'" >
      <input type='text' name='value' value="${value}" />
      <g:submitButton name="search" value="Search" />
    </g:formRemote>
    Results:
    <div id="results">
    
      
    </div>
    </body>
    </html>
    
    _result.gsp
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Sample title</title>
      </head>
      <body>
        <h1>Results</h1>
        <ol>
        <g:each var="book" in="${books}">
          <li>${book?.author}
          <g:if test="${book.title}">- </g:if> ${book?.title}
          </li>
        </g:each>
      </ol>
      </body>
    </html>

  • #2
    Hi, do you want use ajax to show the xml-render ? if that you can try this:
    I use jquery.

    $.get("host/controller/listXml", function(data){
    var books = $(data).find("booklist>book");
    alert(books.size());
    var bookOne = books[0];
    var bookOneTitle = bookOne.firstChild.textContent;
    var bookOneAuthor = bookOne.childNodes[1].textContent;
    alert(bookOneAuthor);
    //you can append these vars to your page.
    //like $("#result").html(bookOneTitle);
    });

    good luck.

    Comment

    Working...
    X