我正在使用AngularJS v1.2.25。当我通过POST将有效负载数据(字符串)发布到我的Web API时,我总是得到null。我用Postman(REST客户端)试了一下,然后我得到了一个字符串。我想知道这是否是一个错误,因为typeof变量是字符串。
这是我在Postman的HTTP请求(使用Fiddler):
POST http://localhost:60858/logout HTTP/1.1
Host: localhost:60858
Connection: keep-alive
Content-Length: 90
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8,is;q=0.6
Cookie: ASP.NET_SessionId=wox5u5pt5k0ox2hkontz53q4; .ASPXANONYMOUS=2CsdBwYO0AEkAAAAOTg3MDRkMjQtZmE4OC00OTE3LWE2NWYtMDk4ODY4MzA0Y2ZlgxJyUfsE9kDSzQ5edZ4FaUrI1djayGn3QfiUK9J4Ko01
"Ge8KFyOASBV8EbbbKJFrj8vkfZy0BYQZmvGrksFTPtesPvIAMvonRyr0qXNJGM18QxROI6s+26L/z/uOhSbJYA=="
这是我的实际HTTP请求(通过AngularJS中的$ http):
POST http://localhost:60858/logout HTTP/1.1
Host: localhost:60858
Connection: keep-alive
Content-Length: 88
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: http://localhost:53998
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:53998/
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8,is;q=0.6
Ge8KFyOASBV8EbbbKJFrj8vkfZy0BYQZmvGrksFTPtesPvIAMvonRyr0qXNJGM18QxROI6s+26L/z/uOhSbJYA==
我使用$ http方法的代码:
return $http[method](fullpath, data);
我在Web API中接收有效负载(字符串)的代码:
[Route("logout")]
[HttpPost]
public bool Logout([FromBody] string accessToken)
{
var logout = _userService.LogUserOut(accessToken);
return logout;
}
答案 0 :(得分:2)
在数据字符串周围添加引号""
。
if(typeof data === 'string') data = '"' + data + '"';