zaxzax
zaxzax

Reputation: 93

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.

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

Upvotes: 1

Views: 5595

Answers (4)

Maissa Jabri
Maissa Jabri

Reputation: 1

One more thing, shouldn't you be calling your controller rather like this:

ng-controller="UpdateController"

Upvotes: 0

bramve
bramve

Reputation: 147

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

As for your question, the $http method expects the data to be given in a javascript object as follows:

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

Upvotes: 1

Immanuel Kirubaharan
Immanuel Kirubaharan

Reputation: 1104

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

Upvotes: 0

ahhmarr
ahhmarr

Reputation: 2320

try passing data like this

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

Upvotes: 0

Related Questions