0

I am using sitecore 9.0 with SXA 1.8.1 I am trying to call a server side method using ajax call. The controller inherits ServicesApiController. The ajax Get method is works fine on CD server. But the post method is give 404 error

Below is the controller code

[System.Web.Http.HttpGet]
        [Route("api/service/sitename/methodname1")]
        public string methodname1()
        {
            try { 
            //some code
 return JsonConvert.SerializeObject(data);
            }
            catch (Exception ex) {

                return string.Empty;

            }
        }

ajax get call for above method. It works fine for both CM and CD server

$.ajax({
        url: "/api/service/sitename/methodname1",
        type: "GET",
        context: this,
        success: function (data) {

        },
        error: function (data) {
        console.log("error", data);
        }
    });

Server side post method

 [System.Web.Http.HttpPost]
 [Route("api/service/sitename/method2")]
 public string method2(string Query)
 {
    try
    {
        //some code
        return JsonConvert.SerializeObject(objResults);
    }
    catch (Exception ex)
    {      
     return null;
    }
  }

ajax post call for the above method. This gives 404 error cd server but works fine in CM

$.ajax({  
           type: 'POST',  
           dataType: 'json',  
           url: '/api/service/sitename/method2',  
           data: { Query: query},  
           beforeSend: function(){
                $('#loadingImage').removeClass("hide");
            },
            complete: function(){
                $('#loadingImage').addClass("hide");
            },
           success: function (searchData) {  

               },  
           error: function (XMLHttpRequest, textStatus, errorThrown) {  

           }  
       });

is there anything missing ?

  • try this link sitecore.stackexchange.com/questions/19716/… – Siva Kumar Sep 25 '19 at 9:43
  • Instead of using api/sitecore default router you need to create a pipeline processor to register your own route , yes the above mentioned link is the correct answer . – D S Sep 25 '19 at 9:57
  • the ajax GET method call worked without any routing configuration. Not sure why its not working for POST method – user3267206 Sep 25 '19 at 10:00

Browse other questions tagged or ask your own question.