【问题标题】:Using Angular to post data to Moodle API使用 Angular 将数据发布到 Moodle API
【发布时间】:2017-01-15 04:09:16
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • Moodle 可以处理 json,但 webservices api 通常需要安全令牌,并且数据参数通常按类型(PARAM_INT 等)进行验证。你在创建一个模块吗?一个主题?最好的方法是复制已经有效的内容并对其进行修改。
  • 嗨@brianlmerritt,我正在创建一个从网络服务收集数据的移动应用程序。因此,Moodle 中没有任何功能可以满足我的需求。
  • 是的,网络服务验证了参数,我正在使用令牌身份验证。
  • 酷 - 所以我建议你在 github.com/moodlehq/moodlemobile2 克隆一个实际的 moodle 移动应用程序,看看他们是如何做到的。都是角1.x
  • uhhmmm 是的,所以我看了看,我不知道他们的应用在做什么......

标签: angularjs json http post moodle


【解决方案1】:

我发现了一个很棒的教程,它确实向服务器提交了角度数据并返回了响应。

$http.post server request in AngularJS 如何在 AngularJS 中使用 HttpPost 方法向服务器发送数据

如果您使用复选框来更改服务器上的数据,您将需要这个Stack Overflow post about how to bind a checkbox to the modelanswer by zsong 为我工作。

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多