1

I'm trying to call a restful service developed in ServiceStack. I've successfully been able to call Get(s), but I'm struggling to call a Put or Post. My script from client.

function savePartner(e) {
            $.ajax({
                type: "PUT",
                contentType: "application/json; charset=utf-8",
                headers: {
                    'X-HTTP-Method-Override': 'PUT'
                },
                url: "http://localhost:49190/test",
                data: partnerInfoToJSON(),
                complete: function (data) { alert("complete"); },
                success: function (data) { alert("done"); },
                error:  function (data) { alert("failed");},
                dataType: "json"
            });
        }

        function partnerInfoToJSON() {
            return JSON.stringify({
                "Name": "TEST"
            });
        };

My test to ensure that the API on the server side is working was done on fiddler and it works.

My Service Code:

[Route("/test/", "Put")]
public class TestDTO
{
    public string Name { get; set; }
}

public class TestDTOResponse
{
    public long ID { get; set; }
    public ServiceStack.ServiceInterface.ServiceModel.ResponseStatus ResponseStatus { get; set; }
}

[EnableCors(allowedMethods: "GET,POST,PUT,DELETE")]
public class TestService : ServiceStack.ServiceInterface.Service
{

    [EnableCors(allowedMethods: "GET,POST,PUT,DELETE")]
    public void Options(TestDTO testDTO)
    { 

    }

    public object Put(TestDTO testDTO)
    {
        try
        {
            return "Hallo World";
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

And the config code:

Plugins.Add(new CorsFeature());

            RequestFilters.Add((httpReq, httpRes, requestDto) =>
                {
                    if (httpReq.HttpMethod == "OPTIONS")
                        httpRes.End();
                });
            base.SetConfig(new EndpointHostConfig
            {
                DebugMode = true,
                DefaultContentType = "application/json",
                GlobalResponseHeaders = {
                        { "Access-Control-Allow-Origin", "*" },
                        { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
                        { "Access-Control-Allow-Headers", "Content-Type, origin, accept" },
                }
            });

Francois Taljaard
  • 1,339
  • 2
  • 12
  • 33

1 Answers1

1

From the jQuery documentation page on the .ajax() method and the "type" parameter:

type (default: 'GET')
Type: String
The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

revlayle
  • 71
  • 5
  • I've got type set to 'PUT', I also set it on overriding the header. – Francois Taljaard Jul 22 '13 at 17:30
  • If you running this in a browser, does the browser support it? If you use a network inspector (like in Firefox's Firebug or Chrome Dev Tools, for example), does it show a PUT request being sent out? Also, is the request having the "Accept: application/json" header being set properly, to ensure the response is in JSON format? If not you may have to put an extra query string parameter of "format=json" in the request. – revlayle Jul 22 '13 at 18:09
  • I've played with all the settings,I can make a call to the client through Fiddler, but not a browser. The problem might be with the way I configure CORS in servicestcak. Did read all the relating topics on stack on the subject and tried all the suggestion but with no luck so far, need to do more reading on servicestack and CORS. – Francois Taljaard Jul 22 '13 at 19:41
  • Ahh, i see that maybe cross domain requests are being blocked then? – revlayle Jul 22 '13 at 19:49
  • OK, I'm stuck. I've tried all the recommendations on the servicestack side to make sure that the API allows cross domain requests without any luck. I want to mention that I'm running both my Rest Service and Web application through Visual Studio designer, this means it uses IIS express. Not sure if this makes a difference? – Francois Taljaard Aug 08 '13 at 09:00
  • Also remember, I can run the Rest service in visual studio and then use fiddler to post and put without any issue. Its only my calling web application thats not happy. – Francois Taljaard Aug 08 '13 at 09:03
  • I know some of the data from this post is possible from the documentation. http://stackoverflow.com/questions/8211930/servicestack-rest-api-and-cors seems to address, *possibly*, your issues. The answer seems to be provoided by the primary maintainer of ServiceStack. – revlayle Aug 12 '13 at 15:58
  • Also, remember, the browser may not support PUT requests via AJAX. Do you get the same problems with GET/POST? – revlayle Aug 12 '13 at 16:05