0

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.

Nealon
  • 2,213
  • 6
  • 26
  • 40
  • Are both the client and server applications on the same domain? I had trouble with that myself once, but never had the opportunity to set it straight. Here's a good resource on it, though: http://stackoverflow.com/questions/20792950/net-web-api-cors-preflight-request – maniak1982 Jun 03 '15 at 18:24
  • Yep, both just going off of my local machine. Like I said, the GET is working fine. – Nealon Jun 03 '15 at 18:25
  • HTTP ERROR 405 is "Method not allowed", so probably IIS or the app must be configured to accept PUT. It may be a configuration error, nothing seems wrong in your code. – pid Jun 03 '15 at 18:28
  • @pid so how would I go about fixing that? – Nealon Jun 03 '15 at 18:29
  • You can change IIS settings to allow that: http://stackoverflow.com/questions/6739124/iis-7-5-enable-put-and-delete-for-restful-service-extensionless – maniak1982 Jun 03 '15 at 18:30
  • I tried removing the argument (string data) from the api and it hits it just fine. It has something to do with how I'm passing that argument. – Nealon Jun 03 '15 at 18:34
  • Maybe try adding: contentType: "text/plain" to your ajax request, as it defaults to: 'application/x-www-form-urlencoded; charset=UTF-8' – ajliptak Jun 03 '15 at 18:39
  • Change your `PUT` to a `POST`. – Rick S Jun 03 '15 at 18:39
  • @RickS albeit this would probably work, I sense here an intent to have a RESTful API, so all those HTTP methods should be in working order. – pid Jun 03 '15 at 18:46
  • @Nealon As @ajiliptak suggested, if you set type to JSON you probably should pass valid JSON. Try replacing the text with `{"message":"Hello, World!"}` or switching to the `text/plain` type. – pid Jun 03 '15 at 18:48
  • My goal is to pass plain text to the PUT. I tried adding a `[FromBody]` to the Web API, and now it's hitting the API, but `data` is `null` – Nealon Jun 03 '15 at 18:53
  • I updated my post to reflect my current attempts... – Nealon Jun 03 '15 at 18:55
  • @nealon dataType is what is expect back from the server, contentType is what you are sending. – ajliptak Jun 03 '15 at 18:57
  • does 'type' need changed to 'method'? – ganders Jun 03 '15 at 18:57

2 Answers2

1

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.

stevanuz
  • 134
  • 5
0
$.ajax({
    type: "PUT", //HTTP VERB
    url: "/api/FTP", //URL
    dataType: 'json', //What type of response you expect back from the server
    contentType: 'text/plain', //What type of data you are trying to send
    data: "Hello World"

})
ajliptak
  • 429
  • 3
  • 7