Phoenix Phoenix - 1 year ago 65
Ajax Question

How to post data on button click using AngularJS

I have an application made with .NET core framework and pure html in the front end. I was using AJAX to post and get data.
I am new to Angular and decided to convert the front end of the application to Angular for learning purposes.

For Example, I have a button that will change the state of employees from 'Billed' to 'Available' state. The ID for available state is defined in the back end and it is '1'.


var allVals = [];

$('input:checkbox:checked').each(function () {
for (i = 0;i<allVals.length;i++){
function PostBenchList(entityId) {
var data = 'entityID='.concat(entityId).concat('&nextStateId=1');

type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:1783/api/Workflow?"+data,
data: data,
dataType: "text",
success: function (data) {
alert("Successfully added the selected Employees to TalentPool");
fail: function (error) {


The above code is taking an array of entityID's as input. For the Angular application, the array is not required as only one entity ID will be passed.

The API controller in the backend is :

// POST api/values
public void Post(int entityId, int nextStateId)
JObject jsonObject = JObject.Parse(System.IO.File.ReadAllText("Config.Json"));
string jsonFile = jsonObject.GetValue("WorkfowJsonFileLocation").ToString();
var nextState = _stateServices.Get(nextStateId);
var handler = new WorkflowHandler(nextState, jsonFile, _entityServices, 1, _stateServices, _subStateServices, _appServices);

The above code worked for me and it would change the state ID of the employee(EntityID)to 1(nextStateId)

Now I have a button in AngularJS and I want it to do the same action. How would I achieve this? As I am still in the procedure of learning, I don't have a clue how to do this. Can anyone help me to achieve this? This would help me to learn and do all similar buttons.

Thank You.

Answer Source

You can use ng-click and call a function to post the data,


  <button ng-click="PostData()">
    Click to POST


   $scope.sendPost = function() {
        var data = $.param({
            json: JSON.stringify({
                name: $scope.newName
        $"/echo/json/", data).success(function(data, status) {
            $scope.hello = data;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download