简体   繁体   中英

Grails formRemote 404 Not Found

I'm having difficulties getting my ajaxform working properly. I'm using Grails 2.4.4 and have the following remoteForm code.

view usedProducts

<div id="updateMe">
    <g:each in="${productList}" var="product">
        ${product.item} Success1
    </g:each>
</div>

<g:formRemote name="myForm"
    url="[controller:'product', action: 'save']" update="updateMe">
    <g:textField name="item" required="" value="${newProduct?.item}" />
    <g:submitButton name="Save" />
</g:formRemote>

Controller ProductController

def usedProducts = {
    [productList:Product.findAll()]
}

@Transactional
def save(Product productInstance) {
    productInstance.save flush:true

    [productList: Product.findAll()]
}

Console Error

POST http://localhost:8080/TekDays/product/save

404 Not Found 54ms
jquery-...e=false (line 9631) "NetworkError: 404 Not Found - http://localhost:8080/TekDays/product/save "

I'm afraid you cannot do that this way you're trying to do.

Note that when the AJAX call is effectively being performed by the user there are no more GSP tags. There are only CSS, HTML and Javascript. It's the browser world.

Please put the render in front of what you're returning and test.

@Transactional
def save(Product productInstance) {
    productInstance.save flush:true    
    render Product.findAll()   
}

You'll see that the content of the DIV updateMe will be replaced by the content rendered by the save method. The content is a list returned by findAll method with all Product objects.

When you use render the content returned (a GSP template, the content of a variable or a HTML) will be rendered in the current HTML, for instance, through an AJAX call.

For other side when you use the return keyword is expected a GSP with the same name as the controller method. Because of that you're getting a 404, since a save.gsp isn't found. Note: if you omit the return keyword (as you're doing) then the groovyc (Groovy compiler) will implicitly put it for you.

If I understand well what you're trying to do then you must render the content (not return it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM