【问题标题】:Why can't my Grails app get an object from my Angular script?为什么我的 Grails 应用程序无法从我的 Angular 脚本中获取对象?
【发布时间】:2015-05-19 13:57:01
【问题描述】:

我正在尝试将对象添加到 Grails 中的 JSON 列表。这是我的 Angular 代码: http://pastebin.com/4ypijUMD

还有我的 Grails 控制器: http://pastebin.com/skTtVtxv

据我了解,我的 angularJS 脚本应该发送一个请求以使用列表中最新成员的信息访问添加控制器。

那么 add 函数应该根据从 angular 脚本和 ta-da 传递到控制器的参数创建一个新的 JSON 对象。

但是,什么都没有创建,当我尝试更新我的时,我只是得到空对象

list with my update controller:
    def saveList() {
        def newItem = Item.findById(request.JSON.id)
        newItem.name = request.JSON.name
        newItem.type = request.JSON.type
        newItem.priority = request.JSON.priority
        newItem.completed = request.JSON.completed
        newItem.save(flush: true)
        render newItem as JSON
    }

【问题讨论】:

    标签: javascript json angularjs grails


    【解决方案1】:

    您没有显示进行实际 AJAX 调用的代码,只是一个带有保存调用的服务。使用 Grails 和 Angular 执行此操作的方式如下所示:

    $http.post('http://localhost:8080/test-app/myController/addItem', { json values})
        .success(function(response) {})
        .error(function(response) {});
    
    class MyController {
        def addItem() {}
    }
    

    【讨论】:

    • 我会用什么代替 json 值?
    【解决方案2】:

    在您过去的bin代码中,我看到了,您从浏览器发送一个列表并处理服务器代码中的单个项目。这可能是问题吗?

    【讨论】:

      【解决方案3】:

      根据您的代码,您没有从角度传递 request.JSON.id。

                                    {                                
                                       id: 1
                                       name: $scope.thing,
                                       type: $scope.worktypeSelection,
                                       priority: $scope.prioritySelection,
                                       completed: false
                                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-16
        相关资源
        最近更新 更多