Laalu Ar

Laalu Ar

  • NA
  • 20
  • 7.8k

Post data from angulerjs client to WEBAPI controller

Jun 16 2016 3:00 AM
hi,
am new in angular js  and webapi 
 
Problem:  
 
i cant post data using json url: "NewRoute/firstCall" from view to WebAPI controller 
 
but i can post using url: "http://localhost:2730/NewRoute/firstCall", 
 
who can i post data without  http://localhost:2730
 
thing is i got two projects in one solution.
1.AngularJS
2.WebAPI
 
 
In view part
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Scripts/angular.js"></script>
<script>
 
var TestCtrl = function ($scope, $http) {
$scope.SendData = function (Data) {
var GetAll = new Object();
GetAll.txt1 = Data.txt1;
$http({
url: "NewRoute/firstCall",
//url: "http://localhost:2730/NewRoute/firstCall",
dataType: 'json',
method: 'POST',
data: GetAll,
headers: {
"Content-Type": "application/json"
}
}).success(function (response) {
$scope.value = response;
})
.error(function (error) {
alert(error);
});
}
};
var myApp = angular.module("myApp", []);
myApp.controller("TestCtrl", TestCtrl);
</script>
</head>
<body ng-app="myApp">
<div ng-controller="TestCtrl">
<hr />
<div>
Enter text
<input type="text" placeholder="Enter your name" ng-model="details.txt1">
</div>
<hr />
<input type="button" ng-click="SendData(details)" value="Submit" />
<hr />
<h4 style="color:blueviolet">{{value}}</h4>
</div>
<hr />
</body>
</html>
 
 In Web Api controller
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace WebApplication1.Controllers
{
[RoutePrefix("NewRoute")]
public class NewController : ApiController
{
public class GetAll
{
public string txt1 { get; set; }
}
[Route("firstCall")]
[HttpPost]
public string firstCall(HttpRequestMessage request,
[FromBody] GetAll getAll)
{
return "Success";
}
}
}
 
 
 
 please help me to solve this issue . thanks in advance
 
 
 

Answers (2)