我试图通过Postman从ajax调用中调用我编写的Web api方法 按网址:“http://localhost:56881/api/Medication”
// POST: api/Medication
[ResponseType(typeof(MedicationViewModel))]
public HttpResponseMessage PostMedication(MedicationViewModel medicationViewModel)
{
if (!ModelState.IsValid)
{
var item = Helper.GetResponseMessage(ApplicationConstats.ErrorStatus, ApplicationConstats.ErrorStatus, null);
return Request.CreateResponse(HttpStatusCode.OK, item);
}
Medication medication = modelviewToModel(medicationViewModel);
db.Medications.Add(medication);
db.SaveChanges();
var response = Helper.GetResponseMessage(ApplicationConstats.Successtatus, ApplicationConstats.Successtatus, null);
return Request.CreateResponse(HttpStatusCode.OK, response);
}
我的电话
$.ajax({
type: 'POST',
url: 'http://localhost:56881/api/Medication',
data: {'MedicationId':44,MedicationName:'dfgsdfgsdfg',DosageFrequency:56,DosageQuantity:464,StartDate:'09/06/2015',EndDate:'09/06/2015',IsActive:1,RefilRemaining:45,TotalQuantity:12,VisitId:4},
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
success: function(data){ console.log(data) }
});
它给我找不到错误资源,请帮帮我
答案 0 :(得分:0)
就像Dean.DePue已经说过的那样,你单独传递参数。 ajax调用一个如下所示的方法:
public HttpResponseMessage Medication(int MedicationId, String MedicationName, int DosageFrequenzy ... etc etc){}
你必须传递一个与你的viewmodel匹配的对象。我假设这些参数代表你的viewmodel。
这将是一种方法:
<script>
var data = "{ MedicationId: 44, MedicationName: xy, DosageFrequenzy: 123 etc.}";
$.ajax({
type: 'POST',
url: 'http://localhost:56881/api/Medication',
data: data,
success: function(data){ console.log(data) }
});
</script>