将数据发布到 JSON - Angular .post

Posting data to JSON - Angular .post

本文关键字:Angular post JSON 数据      更新时间:2023-09-26

我正在处理一个应用程序,但发布到资产中的.JSON文件时遇到问题。 我正在使用基于Angular的应用程序。 我收到的错误代码404响应为:Cannot POST /assets/data/card-stack.json . 现在的问题是,当我使用get来检索JSON数据时,它的效果很好。 只有当我使用.post. 这是我正在做的事情:

$http.get('./../../assets/data/card-stack.json').success(function(data) {
                $scope.cards = data;
                // Set the showdown images from the card data grabbed from the card-stack.json file
                $scope.showdowns = [
                    $scope.cards[0].url,
                    $scope.cards[1].url,
                    $scope.cards[2].url,
                    $scope.cards[3].url
                ];
        });
        // Simple POST request example (passing data) :
        $http.post('./../../assets/data/card-stack.json', {url : './../images/banana.jpg'}).
          success(function(data, status, headers, config) {
            // this callback will be called asynchronously
            // when the response is available
          }).
          error(function(data, status, headers, config) {
              console.log(data);
            // called asynchronously if an error occurs
            // or server returns response with an error status.
         });

建议?

.

json文件是一个静态文件,只包含json数据,因此您将无法发布到该文件。相反,您需要使用服务器端页面或服务(如 php)来处理发布的数据。

尝试设置 $http 标头:

$http.defaults.headers.post["Content-Type"] = "application/json";

试试吧。