0

I'm using jQuery 3.2.1. I'm trying to call API by Ajax jQuery.

I'm following an example and code like this:

(() => {
    function delTest() {
        $.ajax({
            url: 'http://localhost:3413/api/person?ID=100',
            type: 'DELETE',
            dataType: 'json',
            data: { "": "Sourav Kayal" },
            success: function (data, textStatus, xhr) {
                console.log(data);
            },
            error: function (xhr, textStatus, errorThrown) {
                console.log('Error in Operation');
            }
        });
    }
})();

and API

   public class personController : ApiController  
    {  
        [HttpDelete]  
        public string Delete([FromUri] int ID, [FromBody] String name)  
        {  
            return "Delete Operation" + "ID:- " + ID + "Name:- " + name;  
        }  
    }


protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

However, it's not working now.

Returns an error: 404 Not found

12
  • Have you added [RoutePrefix("api/person")] above the public class personController ? Commented Jan 10, 2019 at 8:24
  • Are you sure the hard coded port number of local host is actually the port in use? Commented Jan 10, 2019 at 8:25
  • not yet @Arulkumar Commented Jan 10, 2019 at 8:27
  • the port is not in use @kaffekopp Commented Jan 10, 2019 at 8:27
  • I tried to put [RoutePrefix("api/person")] , still not working Commented Jan 10, 2019 at 8:35

1 Answer 1

1

I am using below code and its working fine on my machine. I think you need to add method name delete in the URL, it should look like "http://localhost:3413/api/person/Delete?ID=100"

<script type="text/javascript">

        function delTest() {

        $.ajax({
            url: 'http://localhost:3413/person/Delete?ID=100',
            type: 'DELETE',
            dataType: 'json',
            data: { name: "Sourav Kayal" },
            success: function (data, textStatus, xhr) {
                console.log(data);
            },
            error: function (xhr, textStatus, errorThrown) {
                console.log('Error in Operation');
            }
        });
    }
</script> 

<input type="button" name="delete" onclick="delTest()" value="makeCall"/>

and API code:

  [System.Web.Http.HttpDelete]
        public string Delete([FromUri] int ID, [FromBody] String name)
        {
            return "Delete Operation" + "ID:- " + ID + "Name:- " + name;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Is this code is working for you? If so can you please mark it as answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.