Search code examples
javascriptc#asp.net-web-apiput

Calling PUT method from Javascript


I'm trying to call my C# Web API from javascript, and the data doesn't appear to be passing correctly. The GET method I have works perfectly fine, but I can't seem to get the PUT working as I intend. Here's the structure:

Javascript:

$.ajax({
    type: "PUT",
    url: "/api/FTP",
    data: "Hello World"
})

C# Web API:

public class FTPController : ApiController
{
    // GET: api/FTP
    public IHttpActionResult Get()
    {
    }

    // PUT: api/FTP
    public void Put([FromBody] string data)
    {
    }
}

Please let me know if I need to provide more info. Thanks for the help.


Solution

  • try data:{'':'Hello World'}

    it will generate =HelloWorld as form data and this is what [FromBody] expects and it's in this case I believe.