Я пытался отправить переменную внутри контроллера JS на мой Java сервер, используя $ http, но я не могу заставить его работать: как только я попробую, я не смогу ввести HTML представление, в котором используется этот код JS, поэтому проблема исходит от контроллера JS. Вот что я делаю и код, основанный на попытке адаптировать эти два ресурса:
документы. angularjs .org / api / ng / service / $ http
angularjs -http-service- ajax -post-code-example
Контроллер:
angular
.module('myApp')
.controller('MessageController', MessageController);
MessageController.$inject = ['$scope', '$stateParams', '$uibModalInstance', 'MessageTemplate', 'JhiLanguageService'];
onFormatChange.$inject = ['$resource', '$http', 'ServerURL']; // should it be vm.onFormatChange.$inject instead?
function MessageController ($scope, $stateParams, $uibModalInstance, MessageTemplate, JhiLanguageService) {
var vm = this;
vm.isSaving = false;
vm.text = 'Activated'; // this is the only variable
// im trying to send to Java
function onSaveSuccess (result) {
// a console.log
}
function onSaveError (error) {
// a console.log
}
// this function gets called from the HTML via ng-change
vm.onFormatChange = function () {
vm.text = vm.status ? 'Deactivated' : 'Activated';
// Sending vm.text (as "format") to the server
$http.post(ServerURL + 'api/format/', {
format: vm.text // should it be format = vm.text?
}. {withCredentials: true,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': true,
'X-Requested-With': 'XMLHttpRequest'
}
}).then(onSaveSuccess, onSaveError);
}
}