Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery RestFul Put request
    text
    copied!<p>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.</p> <p><i> </p> <pre><code>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" }); }; </code></pre> <p></i></p> <p>My test to ensure that the API on the server side is working was done on fiddler and it works.</p> <p>My Service Code:</p> <p><i></p> <pre><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); } } </code></pre> <p></i></p> <p>And the config code:</p> <p><i> Plugins.Add(new CorsFeature());</p> <pre><code> RequestFilters.Add((httpReq, httpRes, requestDto) =&gt; { 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" }, } }); </code></pre> <p></i></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload