AngularJS POST json 到 SilverStripe API
AngularJS POST json to SilverStripe API
我使用 SilverStripe 模块 (here) 编写了一个非常基本的 API 并且在构建它时我正在使用 Postman 和 Advanced REST Client chrome 扩展进行测试所以我知道端点工作。
现在,当尝试到达端点 (POST json) 时,API 告诉我未设置所需的值。我做了一些挖掘,并将来自 Postman 的请求 header 与来自 Angular 使用 Firebug 的请求进行了比较。唯一值得注意的差异是在 Postman 中 Content-Type 的 header 是:
"application/json"
Angular 是:
"application/json; charset=UTF-8"
这是 Angular POST 的代码(非常基本):
notebookFactory.addNotebook = function(title) {
var notebook = "Testing Title 23!";
var message = {
Title: notebook
};
return $http({
url: 'api/notebook',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
data: message
});
};
API 错误说:"The JSON property Title
is required"
这会有所作为吗?我尝试在 Postman 中将 charset=UTF-8 添加到 Content-Type 的末尾并收到相同的错误。有没有办法从 Angular POST header?
中删除字符集
如果您需要更多信息,请告诉我,提前致谢!
这是 RESTful API 模块中处理 json 的代码的一个小问题。
开发者已经修复,可以参考这里:https://github.com/pstaender/silverstripe-restful-api/issues/1
我使用 SilverStripe 模块 (here) 编写了一个非常基本的 API 并且在构建它时我正在使用 Postman 和 Advanced REST Client chrome 扩展进行测试所以我知道端点工作。
现在,当尝试到达端点 (POST json) 时,API 告诉我未设置所需的值。我做了一些挖掘,并将来自 Postman 的请求 header 与来自 Angular 使用 Firebug 的请求进行了比较。唯一值得注意的差异是在 Postman 中 Content-Type 的 header 是:
"application/json"
Angular 是:
"application/json; charset=UTF-8"
这是 Angular POST 的代码(非常基本):
notebookFactory.addNotebook = function(title) {
var notebook = "Testing Title 23!";
var message = {
Title: notebook
};
return $http({
url: 'api/notebook',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
data: message
});
};
API 错误说:"The JSON property Title
is required"
这会有所作为吗?我尝试在 Postman 中将 charset=UTF-8 添加到 Content-Type 的末尾并收到相同的错误。有没有办法从 Angular POST header?
中删除字符集如果您需要更多信息,请告诉我,提前致谢!
这是 RESTful API 模块中处理 json 的代码的一个小问题。
开发者已经修复,可以参考这里:https://github.com/pstaender/silverstripe-restful-api/issues/1