【发布时间】:2016-04-20 15:52:52
【问题描述】:
我正在尝试将数据发布到服务器,但在服务器端,我的模型为空。
数据是
{"email":"adas","password":"sds","grant_type":"password","client_id":"WebApp"}
return $http.post(url, data,{headers: {'Content-Type': 'application/json'} })
.then(function (result) {
success(result);
}, function (error) {
debugger;
//some code
});
但是当我使用简单的 ajax 请求发送它时,我会在服务器端获取我的数据。
var logininfo = { "email": "weee", "password": "password", "client_id": "IosApp", "grant_type": "password" };
$.ajax({
url: "http://localhost:53646/Login",
type: "POST",
data: logininfo,
success: function (data) {
debugger;
}, error: function (daTA) {
debugger;
}
});
还有一个问题,因为我必须向服务器发送一个自定义标头,所以当我在 ajax 请求中设置该标头时,我的数据再次在服务器端为空。
var logininfo = { "email": "weee", "password": "password", "client_id": "IosApp", "grant_type": "password" };
$.ajax({
url: "http://localhost:53646/Login",
headers: { 'ResourceAuthorization': 'Basic Qn12bGV34jEyMzQ1' },
type: "POST",
data: logininfo,
success: function (data) {
debugger;
}, error: function (daTA) {
debugger;
}
});
这就是我尝试获取数据的方式
public void Configuration(IAppBuilder app)
{
app.Use(async (ctx, n) =>
{
if (ctx.Request.Path == new PathString("/Login"))
{
var memstr = new StreamReader(ctx.Request.Body).ReadToEndAsync().Result;
var loginmodel = JsonConvert.DeserializeObject<LoginModel>(memstr);
//some code here
}
await n.Invoke();
});
ConfigureAuth(app);
}
【问题讨论】:
-
你在 Angular 的 $http 中输入:'POST'。它应该是方法:'POST'
-
@MegaRacer
$http.post将在内部执行此操作 -
谢谢@PankajParkar
-
@PankajParkar :我解决了数据问题。但是现在如何在所有请求中发送我的自定义标头。 ?
-
您应该使用 httpIntrerceptor,它会在发送前在每个请求中添加标头
标签: c# angularjs ajax asp.net-mvc asp.net-web-api