c# - HTTP 400 Bad Request - CORS enabled WCF Service -
i trying use cors on wcf cross domain calls service. have of things working, when try call function gives me error -http 400 bad request
i used fiddler capture error , says this
when tried find solutions, saw people suggesting use bodystyle=webmessagebodystyle.bare
. tried , service giving errors because have more 1 parameters.
[operationcontract] [webinvoke(method = "post", bodystyle=webmessagebodystyle.wrapped, requestformat=webmessageformat.json, responseformat=webmessageformat.json, uritemplate = "/getdata")] //[webget(bodystyle = webmessagebodystyle.bare, requestformat = webmessageformat.json, responseformat = webmessageformat.json, uritemplate = "/getdata/{value}/")] string getdata(string value, string val2);
i not sure how solve problem. appreciated.
if need @ more, config please let me know , can share it.
service call:
var datav = "{value : 4, val2 : 5}"; var datasent = json.stringify(datav); $.ajax({ type: "post", datatype: 'json', contenttype: "application/json", data: datasent, url: purl, success: function (data) { alert('success'); }, error: function(xhr, status, error) { alert(xhr.responsetext); } });
first of all, not recommended use post if service returning data. use get instead. still, if going use post, here proper method use in wcf.
[operationcontract] [webinvoke(method = "post", bodystyle=webmessagebodystyle.bare, requestformat=webmessageformat.json, responseformat=webmessageformat.json, uritemplate = "/getdata")] string getdata(myvalues values);
and here myvalues
class.
[datacontract] public class myvalues { [datamember] public string value1{get; set;} [datamember] public string value2{get; set;} public override string tostring() { javascriptserializer js = new javascriptserializer(); // available in system.web.script.serialization; return js.serialize(this); } }
note have written, tostring()
method in myvalues
class. because can format of json send json call. more details here.
when calling ajax, need specify charset
well.
contenttype: "application/json; charset=utf-8",
check now! make sure sending right json in request. tostring()
method return required json format service accept.
Comments
Post a Comment