Seeking assistance with understanding the behavior of $http.post from AngularJS to ASP.NET WebAPI. Although the post is successful, the value received on the API is null. Despite verifying that the $scope object contains a value before posting, the issue persists.
It has become apparent that ASP.NET WebAPI handles posts data in an unconventional manner.
Below is the HTML code used to obtain input in Basic.html:
<form name="basicItem" novalidate ng-app="app" ng-controller="ItemCtrl">
<label id="titlelabel" class="required">Title</label>
<input ng-model="item.Title" type="text" id="titlefield" name="Title" required />
The following snippet from ItemController.js verifies validation and executes post requests using CORS due to separate domains:
app.controller("ItemCtrl", ['$scope', '$http', function($scope, $http) {
$scope.submitForm = function(form) {
if (form.$valid) { //If Title has some value
item = {
"Title": $scope.item.Title, //Set "Title" to the user input
}
alert($scope.item.Title); //This confirms that the value is not null
$http.post("http://localhost:50261",
{
testTitle: $scope.item.Title //Potential issue lies here, setting the parameter for API post
}).success(function(result) {
alert('Success!');
}).error(function(data) {
alert("Valid but didn't connect");
console.log(data);
})
The API controller EntryController.cs comprises the following code snippet:
[HttpPost]
public string CreateEntry([FromBody]string testTitle)
{
return testTitle; //returns null!!!
}
After researching about the necessity of [FromBody] and utilizing only one simple parameter, as well as experimenting with wrapping the post value in quotes or adding a leading "=" sign, none of these methods have yielded success. Any guidance or suggestions would be greatly appreciated.