Why is my REST method not being called by this jQuery?

前端 未结 5 1826
星月不相逢
星月不相逢 2021-01-25 21:32

I have a similar question about jQuery button click handler code not being fired at all here.

In this case, it is being fired (when the jQuery is added to a sta

5条回答
  •  借酒劲吻你
    2021-01-25 22:21

    Personally not a big fan of using @Url.Action in javascript code; this is how I make ajax calls:

    • Create a hidden field in layout page to store the root url -
    • Have a constants json file to keep my client side constants (constants.js) - assigns the hidden field value to serviceRoot variable:

      (function () {
      window.constants = {
      
          serviceRoot: $("#hdnRoot").val() + "api/",
          objectState: {
              added: "Added",
              modified: "Modified",
              unchanged: "Unchanged",
              deleted: "Deleted"
          },
         ..other values...
      };
      })();
      
    • Will modify your ajax call as:

      var root = window.constants.serviceRoot;
      $.ajax({
                  type: 'GET',
                  url: root + 'LandingPage/GetQuadrantData/'+unitval+'/'+begdateval+'/'+enddateval,
                //  data: { unit: unitval, begdate: begdateval, enddate: enddateval },
                  contentType: 'application/json',
                  cache: false,
                  success: function (returneddata) {
                  },
                  error: function () {
                      alert('hey, boo-boo!');
                  }
              });
      
       [RoutePrefix("api")]
       public class LandingPageController : ApiController {
      
         [HttpGet]
         [Route("GetQuadrantData/{unit}/{begdate}/{enddate}", Name="GetQuadrantDataFromLandingPage")]
         public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate) 
      

提交回复
热议问题