Click here to Skip to main content
15,915,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
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


What I have tried:

i tried alot but am new to angular jS and webApi so i cant get proper way to get result
Posted
Comments
Vindhyachal_Kumar 16-Jun-16 4:12am    
You can't do it because your angularjs and webapi projects are running on two different port.

Alternatively you can try url rewriting to reroute your request to webapi.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900