【问题标题】:AngularJS POST json to SilverStripe APIAngularJS POST json 到 SilverStripe API
【发布时间】:2015-11-06 03:55:44
【问题描述】:

我使用 SilverStripe 模块 (here) 编写了一个非常基本的 API,并且在构建它时我正在使用 Postman 和 Advanced REST Client chrome 扩展进行测试,因此我知道端点可以工作。

现在,当尝试到达端点(POST json)时,API 告诉我没有设置所需的值。我做了一些挖掘,并将 Postman 的请求标头与使用 Firebug 的 Angular 的请求标头进行了比较。唯一明显的差异是 Postman 中 Content-Type 的标头是:

“应用程序/json”

对于 Angular,它是:

"应用程序/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 错误提示:“JSON 属性 Title 是必需的”

这会有所作为吗?我尝试在 Postman 的 Content-Type 末尾添加 charset=UTF-8 并收到相同的错误。有没有办法从 Angular POST 标头中删除字符集?

如果您需要更多信息,请告诉我,并提前致谢!

【问题讨论】:

  • 您能否显示来自 Network 的来自 Angular 的 POST 请求的 Request Payload(在 Params 选项卡下)控制台?

标签: json angularjs rest silverstripe


【解决方案1】:

这是 RESTful API 模块中处理 json 的代码的一个小问题。

开发者已经修复了,可以在这里参考:https://github.com/pstaender/silverstripe-restful-api/issues/1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多