AngularJs和Web API-无法使用$http.post检索post数据
这是我的web api方法:AngularJs和Web API-无法使用$http.post检索post数据,angularjs,http,post,asp.net-web-api,asp.net-ajax,Angularjs,Http,Post,Asp.net Web Api,Asp.net Ajax,这是我的web api方法: // POST api/Account/Register [AllowAnonymous] [Route("Register")] public async Task<HttpResponseMessage> Register([FromBody] string data) { //data is always null } 结果: WebApi无法识别任何数据或返回0。。。。这就是fidd
// POST api/Account/Register
[AllowAnonymous]
[Route("Register")]
public async Task<HttpResponseMessage> Register([FromBody] string data)
{
//data is always null
}
结果:
WebApi无法识别任何数据或返回0。。。。这就是fiddler在我的帖子中提到的:
POST http://localhost:65337/api/Account/Register HTTP/1.1
Host: localhost:65337
Connection: keep-alive
Content-Length: 82
Accept: application/json, text/plain, */*
Origin: http://localhost:65337
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36
Content-Type: application/json
Referer: http://localhost:65337/WebApp/index.html
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
0=%22&1=%5B&2=o&3=b&4=j&5=e&6=c&7=t&8=+&9=O&10=b&11=j&12=e&13=c&14=t&15=%5D&16=%22 // <- Not sure what my post data is formatted like so...
任何帮助或指导都将不胜感激。谢谢您需要对数据进行编码,请参阅我关于如何使用的文章。为数据分配一个json对象,而不是json.stringfy中的内容$http将在内部进行字符串化。不要使用引号。我尝试过将postObj分配给数据,但没有使用stringify或strings,但postObj是正确的Json对象吗?如果没有,有什么建议吗?很好的帖子,qq,我正在发送用户名/pw作为我的数据,因此我无法通过URL编码发送数据,WebAPI是否无法从$http.post正文中提取数据?请参阅本文,它应该可以帮助您找到错误所在
POST http://localhost:65337/api/Account/Register HTTP/1.1
Host: localhost:65337
Connection: keep-alive
Content-Length: 82
Accept: application/json, text/plain, */*
Origin: http://localhost:65337
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36
Content-Type: application/json
Referer: http://localhost:65337/WebApp/index.html
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
0=%22&1=%5B&2=o&3=b&4=j&5=e&6=c&7=t&8=+&9=O&10=b&11=j&12=e&13=c&14=t&15=%5D&16=%22 // <- Not sure what my post data is formatted like so...