Answer the question
In order to leave comments, you need to log in
How to make $http.post work with web.api?
Hello. Knowledgeable and able, please tell me what the problem is. I'm struggling for the second day on sending data from the Angular input field to the Web.Api method.
WEB.API:
public class SVController : ApiController
{
private string message = "Enter your username and password";
// GET: api/SV
public string Get(int id)
{
return message;
}
// GET: api/SV/5
public string Get()
{
return message;
}
// POST: api/SV
public void Post([FromBody]string value)
{
message = value;
}
// PUT: api/SV/5
public void Put(int id, [FromBody]string value)
{
message = value;
}
}
}
Angular:
var loginApp = angular.module("loginApp", []);
loginApp.controller("loginController", function ($scope, $http) {
$http({ method: 'POST', url: ' localhost:55623/api/sv ', data: "123" }).then(function (response) {
})
$http({ method: 'GET', url: ' localhost:55623/api/SV ' }).then(function (response) {
$scope.message = response.data;
})
})
The response to POST is zero. Get returns the original value of the string "Enter your username and password"
Answer the question
In order to leave comments, you need to log in
1) Requests in AngularJS are asynchronous
2) Requests are sent with Content-Type: application/json - make sure the server handles correctly
C post partially
parsed WebApi
[HttpPost]
public string Post([FromBody]string value)
{
}
Angular
$scope.Send = function (login, password) {
$http.post(' localhost:55623/api/sv ', login + "." + password).then(function (response) {
$scope.message = response.data;
});
Now the problem is the following - I send only numbers through post, if login or Password contains one or more letters, then the value in the web api comes empty.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question