AngularJs邮政的WebAPI总是空 [英] AngularJs Post to WebApi is always null
问题描述
角code:
getAuthorizationStatus:功能(){
变种推迟= $ q.defer();
$ HTTP({
方法:POST,
网址:网址,
数据:{
用户名:$ scope.username,
密码:$ scope.password
},
的contentType:应用/ JSON,
标题:{
内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8
}
})。成功(deferred.resolve)
.error(deferred.reject);
返回deferred.promise;
},
我的服务器端code:
[HttpPost]
公众诠释的ValidateUser([FromBody]凭证的凭证)
{
尝试
{
字符串的用户名= credentials.username;
字符串密码= credentials.password;
//做东西
}
赶上(异常前)
{
扔恩;
返回-1;
} 返回-1; //无效的用户
}
我遇到的问题是,我能够打API方法,但里面的数据总是空。我曾尝试这样几种组合:
数据:JSON.stringify({
用户名:用户名,
密码:输入mypassword
}),
没有骰子。
我是什么在错误行为?
我反而试图改变 $ HTTP
的POST的默认和适当的行为,反而让服务器从正确的地方读取数据。从 MVC控制器摘自:从HTTP身体得到JSON对象?
[HttpPost]
公众的ActionResult指数(INT?ID)
{
流REQ = Request.InputStream;
req.Seek(0,System.IO.SeekOrigin.Begin);
JSON字符串=新的StreamReader(REQ).ReadToEnd(); InputClass输入= NULL;
尝试
{
//假设JSON.net/Newtonsoft库从http://json.$c$cplex.com/
输入= JsonConvert.DeserializeObject< InputClass>(JSON)
} 赶上(异常前)
{
//尝试和处理畸形的POST体
返回新的HTTPStatus codeResult(的HTTPStatus code.BadRequest);
} //做东西}
原来,MVC并没有真正的POST体绑定到任何
特定类。你也不能只取POST身体的参数
在的ActionResult(在另一个答案的建议)。很公平。你需要
从请求获取视频流式传输自己和处理它。
块引用>Angular Code:
getAuthorizationStatus: function () { var deferred = $q.defer(); $http({ method: "POST", url: url, data: { username: $scope.username, password: $scope.password }, contentType: 'application/json', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } }).success(deferred.resolve) .error(deferred.reject); return deferred.promise; },
My Server side code:
[HttpPost] public int ValidateUser([FromBody]Credentials credentials) { try { string username = credentials.username; string password = credentials.password; //Do stuff } catch (Exception ex) { throw ex; return -1; } return -1; // not valid user }
The problem I am having is I am able to hit the Api Method but the data inside is always null. I have tried several combinations like this:
data: JSON.stringify({ "username" : "username", "password":"mypassword" }),
No dice.
What am I doing in wrong ?
解决方案I would instead trying to change the default and appropriate behavior of
$http
's POST, instead let the server read the data from the right place. Taken from MVC controller : get JSON object from HTTP body?:[HttpPost] public ActionResult Index(int? id) { Stream req = Request.InputStream; req.Seek(0, System.IO.SeekOrigin.Begin); string json = new StreamReader(req).ReadToEnd(); InputClass input = null; try { // assuming JSON.net/Newtonsoft library from http://json.codeplex.com/ input = JsonConvert.DeserializeObject<InputClass>(json) } catch (Exception ex) { // Try and handle malformed POST body return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } //do stuff }
It turns out that MVC doesn't really bind the POST body to any particular class. Nor can you just fetch the POST body as a param of the ActionResult (suggested in another answer). Fair enough. You need to fetch it from the request stream yourself and process it.
这篇关于AngularJs邮政的WebAPI总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!