我试图从ajax调用调用我编写的Web api Post方法(即postmedication),它给我错误“找不到资源”

本文关键字:调用 postmedication 找不到资源 资源 找不到 错误 方法 ajax Post api Web | 更新日期: 2023-09-27 18:01:45

我试图通过邮差从ajax调用调用我编写的Web api方法通过URL: http://localhost: 56881/api/药物"

  // 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) }
});

提示错误资源未找到,请帮助我

我试图从ajax调用调用我编写的Web api Post方法(即postmedication),它给我错误“找不到资源”

像Dean一样。副警长已经说过了,你要逐个传递参数。ajax调用的方法如下所示:

public HttpResponseMessage Medication(int MedicationId, String MedicationName, int DosageFrequenzy ... etc etc){}

你必须传递一个匹配你的视图模型的对象。我假设这些参数代表您的视图模型。

这将是一个方法:

<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>