使用Angular将数据发布到Moodle API

时间:2023-02-05 12:55:29

According to the answers to previous questions about posting data back to a server API with Angular, you need to post the data as a plain-text string instead of as Json.

根据以前关于使用Angular将数据发布回服务器API的问题的答案,您需要将数据发布为纯文本字符串而不是Json。

However, I am thinking that there is a built-in function within the Moodle library that already deals with Json posted to it, in which case I should just be able to send Json. Is that correct?

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

And if it is correct, should I url-encode my Json string?

如果它是正确的,我应该对我的Json字符串进行url编码吗?

This is my function so far, which is inside my controller.

到目前为止,这是我的功能,它位于我的控制器内部。

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' }
            ];
      });
   } 
});

1 个解决方案

#1


1  

I found a brilliant tutorial which does submit angular data to the server and return a response.

我找到了一个很棒的教程,它将角度数据提交给服务器并返回响应。

$http.post server request in AngularJS How to send data to server using HttpPost method in AngularJS

AngularJS中的$ http.post服务器请求如何使用AngularJS中的HttpPost方法将数据发送到服务器

And if you are using a checkbox to change the data on the server, you will need this Stack Overflow post about how to bind a checkbox to the model. The answer by zsong worked for me.

如果您使用复选框来更改服务器上的数据,则需要有关如何将复选框绑定到模型的Stack Overflow帖子。 zsong的答案对我有用。

#1


1  

I found a brilliant tutorial which does submit angular data to the server and return a response.

我找到了一个很棒的教程,它将角度数据提交给服务器并返回响应。

$http.post server request in AngularJS How to send data to server using HttpPost method in AngularJS

AngularJS中的$ http.post服务器请求如何使用AngularJS中的HttpPost方法将数据发送到服务器

And if you are using a checkbox to change the data on the server, you will need this Stack Overflow post about how to bind a checkbox to the model. The answer by zsong worked for me.

如果您使用复选框来更改服务器上的数据,则需要有关如何将复选框绑定到模型的Stack Overflow帖子。 zsong的答案对我有用。