A
A
Anton2015-06-26 13:20:26
Angular
Anton, 2015-06-26 13:20:26

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

2 answer(s)
M
Mikhail Osher, 2015-06-26
@miraage

1) Requests in AngularJS are asynchronous
2) Requests are sent with Content-Type: application/json - make sure the server handles correctly

A
Anton, 2015-06-26
@Anton_iv87

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 question

Ask a Question

731 491 924 answers to any question