我有一個看起來像這樣一個jQuery的方法:發佈至網絡API參數爲null
$.post("/api/amazon/signature", { "policy": policy }, function (data) {
console.log(data);
});
API方法看起來像這樣〜:
// POST api/amazon/signature
[HttpPost]
[Route("api/amazon/signature")]
public IHttpActionResult GetSignature([FromBody]string policy)
{
var bKey = Encoding.ASCII.GetBytes(ConfigurationManager.AppSettings["AWSSecretKey"]);
var hmacSha1 = new HMACSHA1(bKey);
var bPolicy = Encoding.ASCII.GetBytes(policy);
var hash = hmacSha1.ComputeHash(bPolicy);
var encoded = Convert.ToBase64String(hash);
return Ok(encoded);
}
但是當我運行這段代碼政策始終爲空! 如果我改變我的方法是:
public class Signature
{
public string Policy { get; set; }
}
// POST api/amazon/signature
[HttpPost]
[Route("api/amazon/signature")]
public IHttpActionResult GetSignature([FromBody]Signature model)
{
var bKey = Encoding.ASCII.GetBytes(ConfigurationManager.AppSettings["AWSSecretKey"]);
var hmacSha1 = new HMACSHA1(bKey);
var bPolicy = Encoding.ASCII.GetBytes(model.Policy);
var hash = hmacSha1.ComputeHash(bPolicy);
var encoded = Convert.ToBase64String(hash);
return Ok(encoded);
}
和修改我的jQuery這樣的:
$.post("/api/amazon/signature", { "Policy": policy }, function (data) {
console.log(data);
});
它工作得很好....
有人能告訴我爲什麼嗎?
感謝您的詳細解釋 – r3plica