데이터를 가져오고 게시 할 작업이있는 API 컨트롤러가 있습니다. 그리고 Ajax bu productId 매개 변수에서 GetProductById meyhod에 액세스하려고하면 항상 0과 같습니다. GET 메서드로 GetAll 메서드를 호출하고 싶을 때 모든 것이 좋습니다. 자바 스크립트 코드는ASP.NET Core API 매개 변수가 null입니다.
var product = JSON.stringify({ productId: id });
$http({
method: 'POST',
contentType: 'application/json',
url: '/api/Product/ProductInfo',
data: product
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
아래처럼 그리고 API 컨트롤러는
[Route("api/Product")]
public class ProductController : BaseController
{
[HttpPost]
[Route("ProductInfo")]
public Product GetProductById([FromBody]int productId)
{
return UnitOfWork.GetRepository<Product>().FirstOrDefault(i => i.Id == productId);
}
[HttpGet]
[Route("GetAll")]
public string GetAll()
{
return "Can";
}
}
사람이 그런 경우가 발생하는 이유를 말할 수 아래처럼?
, 본문으로 123을 사용합니다. 나도 path라는 URL을 불러야하지 않습니까? productId = 1도 아니고 기본 유형 대신 참조 유형을 사용해야합니다. –
차갑다. 그것은 본질적으로 그것은 주어진 게시물의 주어진 권리를 얻는 방법입니다. :) 이러한 경우에는 keyvalue 쌍을 사용할 수 있지만 어쨌든 추가 수업은 필요하지 않습니다. –
KeyValuePair? json에서 어떻게 전송할 수 있습니까? 아니면 API 작업에서 가져올 수 있습니까? –