0

I'm working on an MVC app and I'm trying to make a AJAX connection to an web API controller I have. The only error I can surface is "Unidentified". Also In my api controller delete action I'm throwing a random exception so I can detect if I successfully get into the method but I don't.

UPDATE:

Ok It seems I was forgetting the "removeFile" route on my url in the ajax call but the issue is If I add on a "data" attribute to the ajax call to pass in my serialized form controls it stops working properly.

data: $("#frmManipulate").serialize()

Ajax call:

$("#frmManipulate").submit(function () {
        if (confirm("Are you sure you want to delete all selected files?")) {
            if ($(this).data('clicked').is('[name="delete"]')) {
                $.ajax({
                    type: "DELETE",
                    url: "api/file/removeFile",
                    data: $("#frmManipulate").serialize()
                }).complete(function ($data) {
                    alert("done");
                });
        }
        return false;
    });

Web API Controller:

[RoutePrefix("api/file")]
public class ManipulateController : ApiController
{
    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/<controller>
    public void Post([FromBody]string value)
    {
    }

    // PUT api/<controller>/5
    public void Put(int id, [FromBody]string value)
    {
    }

    [Route("removeFile")]
    // DELETE api/<controller>/5
    public void DeleteFile(string[] url)
    {
        foreach (var file in url)
        {
            throw new MarshalDirectiveException();
        }
    }
}

1 Answer 1

1

Try making DELETE request without a message body.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried that but it didn't work. And either way I want to pass my form controls to it.
try adding the [HttpDelete] attribute to the controller method, and then try doing a delete request
DELETE cannot have a message body.

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.