gpt4 book ai didi

angularjs - POST请求有效(状态200),但满足失败条件

转载 作者:行者123 更新时间:2023-12-02 16:01:27 25 4
gpt4 key购买 nike

这是我的AngularJS代码发出的请求:

$scope.add = function(){    // Function used to add a value to list. Value has name, type, and priority.
if($scope.thing){
$scope.items.push(
{name: $scope.thing,
type: $scope.worktypeSelection,
priority: $scope.prioritySelection,
completed: false,
comments: '',
creator: $scope.username,
assignedTo: $scope.assign
});

var itemToJson = $scope.items[$scope.items.length - 1];

$http.post('http://localhost:8080/server/todoList/addItem', {itemToJson})
.success(function(response) {alert("success");})
.error(function(response) {alert("fail");});

$scope.thing = '';
}
else{
alert("Empty task is not allowed!");
}
};

还有我的Grails Controller :
package server

import grails.converters.JSON

class TodoListController {

def addItem() {
def newItem = new Item(request.JSON)
newItem.save(flush: true)
render newItem as JSON
}

def index(){
render Item.list() as JSON
}
}

及其域类:
package server

class Item {

String name
String type
String priority
boolean completed = false
String comments
String creator
String assignedTo

static constraints = {
name nullable: false
priority nullable: true
type nullable: true
comments nullable: true
creator nullable: false
assignedTo nullable: false
}
}

当我启动该帖子时,我从网络上收到200(据说这意味着该帖子还可以),但是随后我得到了失败结果(警告弹出并显示“失败”)。如果请求实际上到达了服务器,我不明白问题是什么

最佳答案

项目ToJson已经是json对象,
不要放在“{}”之间
像这样:

$http.post('http://localhost:8080/server/todoList/addItem', itemToJson)

关于angularjs - POST请求有效(状态200),但满足失败条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30363922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com