我有这个web api控制器:
public class LoginController : ApiController
{
private mw2devnew15Entities db = new mw2devnew15Entities();
[System.Web.Http.HttpGet]
public string Post()
{
string authenticationToken = "";
return authenticationToken;
}
[System.Web.Http.AcceptVerbs("GET", "POST")]
public HttpResponseMessage Post(JObject data)
{
dynamic json = data;
LoginForm loginF = new LoginForm();
loginF.username = json.username;
loginF.password = json.password;
return Request.CreateResponse(HttpStatusCode.OK);
}
}
我可以使用此ajax调用正确发布:
jQuery.ajax({
type: "POST",
url: "http://localhost:5832/api/Login",
data: JSON.stringify({ username: 'joep11aul1234', password: '1212213' }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
}
});
但是当我尝试使用Postman进行POST调用时,JObject为null。
知道为什么吗?
答案 0 :(得分:3)
使用Postman,您不会再生成与JavaScript代码相同的请求,因为您在查询字符串中发布了参数。你应该做的是这样的事情:
添加内容类型标头,其值为application/json
:
并为您的请求正文选择raw
,然后添加您的JSON:
这将发送与JavaScript代码相同的以下请求:
POST /api/Login HTTP/1.1
Host: localhost:5832
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 4bf25ded-7548-77f9-3389-fa16a5d50087
{ "username": "joep11aul1234", "password": "1212213" }