简体   繁体   English

如何使用 angularjs 发布 JSON 数据

[英]How to post JSON data with angularjs

I am trying to post some labels with angularjs, but i am very new to it and dont seem to get it working.我正在尝试使用 angularjs 发布一些标签,但我对它很陌生,似乎无法正常工作。

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

I would highly recommend starting out learning angularjs from the beginning, like how it's DI works, how their scopes and data binding work since you have more issues that just an invalid way of posting我强烈建议从头开始学习 angularjs,比如它是如何工作的,它们的作用域和数据绑定是如何工作的,因为你有更多的问题,而这只是一种无效的发布方式

As for your question, the $http method expects the data to be given in a javascript object as follows:至于你的问题, $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');
});

try passing data like this尝试像这样传递数据

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

You try somethings like the below code,你尝试类似下面的代码,

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

One more thing, shouldn't you be calling your controller rather like this:还有一件事,你不应该像这样调用你的控制器:

ng-controller="UpdateController" ng-controller="更新控制器"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM