0

我正在从远程网页发布到 Azure 上的 Web API。我使用以下内容:

    $.ajax({
        type: "POST",
        url: BaseHref + "/api/PIRknownPersons/",
        data: data,
        contentType: "application/json; charset=UTF-8",
        success: function (response) {
            //alert('Saved known ');
        },
        error: function (e) {
             //alert('Unable to save Known ' );
        }
    });

我可能是错的,但看起来远程发布在 Azure 上被阻止,因为当我在本地执行代码时,代码运行良好。是这样吗?Azure 上是否有可以关闭的设置?

PIRknownPersons -

namespace TripWeb.Controllers
{
    public class PIRknownPersonsController : ApiController
    {
        private TripDB3Entities db = new TripDB3Entities();

        // POST: api/PIRknownPersons
        [ResponseType(typeof(bool))]
        public HttpResponseMessage Post([FromBody]ApiPIRknownPerson APIPIRknownPerson)
        {
            var inst = db.Institutions.Where(x => x.RemoteID == APIPIRknownPerson.RemoteID).FirstOrDefault();
            if (inst == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Action Not Authorized");
            }

            var newPIRknownPerson = ObjectMapper.GetEfApiPIRknownPerson(APIPIRknownPerson);

            try
            {
                db.PIRknownPersons.Add(newPIRknownPerson);
                db.SaveChanges();
                return Request.CreateResponse<bool>(HttpStatusCode.OK, true);
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
            }
        }

    }
}
4

1 回答 1

0

I found the problem - or at least I got it working. I did two things:

  • Enabled CORS on Azure through the control panel
  • Appended [System.Web.Http.AcceptVerbs("GET", "POST")] to the top of my controller files.

Don't know if both are needed but it works now anyway.

于 2020-01-23T22:50:55.313 回答