rest - JQuery RestFul Put request -
i'm trying call restful service developed in servicestack. i've been able call get(s), i'm struggling call put or post. script 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 ensure api on server side working done on fiddler , 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 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" }, } });
from jquery documentation page on .ajax() method , "type" parameter:
type (default: 'get')
type: string
type of request make ("post" or "get"), default "get". note: other http request methods, such put , delete, can used here, but not supported browsers.
Comments
Post a Comment