【问题标题】:How to post JSON data with angularjs如何使用 angularjs 发布 JSON 数据
【发布时间】:2018-05-28 06:12:16
【问题描述】:

我正在尝试使用 angularjs 发布一些标签,但我对它很陌生,似乎无法正常工作。

angular.module('labelsAdmin')
    .component('updateManagement', {
        controllerAs: 'ctrl',
        template: require('./update.html'),
        controller: ['$http', UpdateController]
    });

【问题讨论】:

    标签: javascript angularjs json twitter-bootstrap post


    【解决方案1】:

    我强烈建议从一开始就学习 angularjs,比如它的 DI 是如何工作的,它们的范围和数据绑定是如何工作的,因为你有更多的问题,只是一种无效的发布方式

    至于您的问题,$http 方法期望数据以 javascript 对象的形式给出,如下所示:

    $http({
        method: 'POST',
        url: 'http://localhost:8080/api/labels/',
        data: {
            key: 'key',
            subkey: 'subkey',
            et: 'et',
            ru: 'ru',
            en: 'en',
            desc: 'desc'
        }
    }).then(function successCallback(response) {
        console.log(response);
        getData(response);
        // console.log(response.data[0].key);
        // console.log(response.data[0].tkTextValues[0].text);
        // console.log(response.data.length)
    }, function errorCallback(response) {
        console.log('error');
    });
    

    【讨论】:

      【解决方案2】:

      尝试像这样传递数据

      $http({{
      method : 'POST',
      data : {
      // your full json payload that you want to send
      }
      })
      

      【讨论】:

        【解决方案3】:

        你试试下面的代码,

        var params = {
            key: 'key',
            subkey: 'subkey',
            et: 'et',
            ru: 'ru',
            en: 'en',
            desc: 'desc'
        }
        $http({
            method: 'POST',
            url: 'http://localhost:8080/api/labels/',
            data: params
        }).then(function(response) { 
            //Success
            console.log(response.data);
        }, function(response){
            //Exception
            console.log(response);
        });
        

        【讨论】:

          【解决方案4】:

          还有一件事,你不应该像这样调用你的控制器吗:

          ng-controller="UpdateController"

          【讨论】:

          • @MathiasGhys 下次我一定会发布完整的答案 :)
          猜你喜欢
          • 2015-01-29
          • 1970-01-01
          • 1970-01-01
          • 2021-03-17
          • 2015-05-05
          • 2011-02-24
          • 2011-09-09
          • 1970-01-01
          相关资源
          最近更新 更多