1

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.

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?

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

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' }
            ];
      });
   } 
});
Community
  • 1
  • 1
Yvonne Aburrow
  • 2,602
  • 1
  • 17
  • 47
  • 1
    Moodle can handle json, but the webservices api typically would require a security token, and the data parameters normally are validated by type (PARAM_INT etc). Are you creating a module? a theme? The best way to proceed is to copy something that already works and modify that. – brianlmerritt Sep 13 '16 at 14:05
  • hi @brianlmerritt, I am creating a mobile app which collects data from a webservice. So there aren't any functions in Moodle which do what I want. – Yvonne Aburrow Sep 14 '16 at 09:51
  • And yes the webservice validates the parameters, and I am using token authentication. – Yvonne Aburrow Sep 14 '16 at 09:52
  • 1
    Cool - so I suggest you clone a copy of the actual moodle mobile app at https://github.com/moodlehq/moodlemobile2 and have a look to see how they do it. It's all angular 1.x – brianlmerritt Sep 14 '16 at 10:23
  • uhhmmm yeah so I had a look and I have no idea what their app is doing... – Yvonne Aburrow Sep 14 '16 at 10:39

1 Answers1

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

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.

Community
  • 1
  • 1
Yvonne Aburrow
  • 2,602
  • 1
  • 17
  • 47