使用 Angular 到 post 数据到 Moodle API

Using Angular to post data to Moodle API

根据之前关于posting data back to a server API with Angular, you need to post the data as a plain-text string instead of as Json的问题的回答。

但是,我认为 Moodle 库中有一个内置函数已经处理了发布到它的 Json,在这种情况下我应该能够发送 Json .对吗?

如果 正确,我是否应该 url 编码我的 Json 字符串?

这是我目前的功能,它在我的控制器中。

myApp.controller('mydataCtrl', function ($scope, $http) {
url = concatUrl + 'local_webservice_ws_get_mydata';          // GET
updateUrl = concatUrl + 'local_webservice_ws_set_mydata';    // SET
$http.get(url).then(function (response) {
    $scope.mydata = response.data;
});

$scope.sendmypost = function () {
    // Writing it to the server

    $http.post(updateUrl, $scope.mydata).then(function (response) {
        // Success
        $scope.server_response = [
                    { 'message':'Your settings have been updated' }
            ];
    }, function (response) {
        // Error
        $scope.server_response =  [
                    { 'message':'Unexpected error' }
            ];
      });
   } 
});

我找到了一个很棒的教程,它确实向服务器提交 angular 数据并 return 响应。

$http.post server request in AngularJS AngularJS

中如何使用HttpPost方法向服务器发送数据

如果您使用复选框更改服务器上的数据,您将需要这个 Stack Overflow post about how to bind a checkbox to the model. The answer by zsong 对我有用。