控制器返回404错误,即使它返回结果

时间:2012-09-05 04:40:06

标签: grails

我的URLMappings中有以下映射

addPurchase:"/addPurchase" {
            controller = "cart"
            action = "addPurchase"
        }

我从我的GSP进行AJAX调用

function addPurchase(purchaseJSON, amount) {
            $.ajax({
                url: 'addPurchase',
                data: {
                    purchase: purchaseJSON
//                    quantity: amount
                },
                type : "POST",
                complete:function(data) {
                    updateCart();
                    updateOrderSummary(data);
                }
            });
        }

我的控制器代码是

def addPurchase = {

      def result = cartService.addPurchase(session,params)

      log.debug"Results from addPurchase" + result
      response.status = 200
      return result as JSON

    }

虽然我在控制台中看到了输出,但我的浏览器中出现错误404。为什么会这样?我的grails版本是@ 1.3.7

1 个答案:

答案 0 :(得分:12)

您实际上是在返回结果,而所需的回复是'text / html'或'text / json'

因此,在您的控制器中,您应该render结果而不是return;喜欢:

def addPurchase = {
    def result = cartService.addPurchase(session,params)
    log.debug"Results from addPurchase" + result
    response.status = 200
    render result as JSON
}