无法在Angular中发布数据

时间:2014-09-15 16:30:15

标签: javascript angularjs form-submit

这是我的html页面,它是一个基本表单,其中包含用于安排测试的详细信息列表

<div class="container panel">
  <form ng-submit="submit(testConfigForm)">
        <input type="text" name="testName" ng-model="test.testName"> Test Name <br/>
        <input type="date" name="testDate"  ng-model="testConfigForm.testDate"> Test Date <br/>
        <input type="number" min="0" max="100" name="testTargetScore"  ng-model="testConfigForm.testScore"> Test Score <br/>

        <span ng-repeat="unit in units">
            <label><input type="checkbox" name="checkbox" ng-model="testForm.chapters[$index]">{{unit.name}}</label><br>
        </span>
        <input type="submit"  value="Save">
    </form>


这就是我的控制器看起来像


app.controller('TestConfigurationCtrl', ['$scope', '$routeParams', '$sce', '$http', 'chapterService', 
    function ($scope, $routeParams, $sce, $http, unitService) {

    $scope.chapters = unitService.getUnits();
    $scope.postUrl = $sce.trustAsResourceUrl($routeParams.content_item_return_url);

    $scope.submit = function(testConfigForm) {

        var config = {
            "@context" : [],
            "@graph" : [ {
                "@type" : "ContentItemPlacement",
                "placementOf" : {
                    "@type" : "LtiLink",
                    "text" : "test",
                    "mediaType" : "application/vnd.ims.lti.v1.launch+json",
                    "title" : "Test Exam",
                    "custom" : {
                        "name" : testConfigForm.testName,
                        "dueDate" : testConfigForm.testDate,
                        "targetScore" : testConfigForm.testTargetScore,
                        "chapters" : testConfigForm.units
                    },
                    "activityType" : "abc",
                    "activityId" : "1"
                }
            } ]
        };

        $http.post($scope.postUrl, config)
        .success(function (data, status, headers, config)
        {
          $scope[resultVarName] = data;
        })
        .error(function (data, status, headers, config)
        {
          $scope[resultVarName] = "SUBMIT ERROR";
        });
    };

}]);
});

如何从表单发布数据。它给我一个加载资源失败的错误

1 个答案:

答案 0 :(得分:1)

我想我发现了问题,是这样的:

$scope.postUrl = $sce.trustAsResourceUrl($routeParams.content_item_return_url);
...
$http.post($scope.postUrl, config)

你不应该在帖子上使用$ scope.postUrl,按原样使用url:

var postUrl = $routeParams.content_item_return_url;
$scope.postUrl = $sce.trustAsResourceUrl(postUrl);
...
$http.post(postUrl, config)