jquery - Sending JSON object to WCF Rest Service -
i trying application working using rest, wcf , json (new technologies). have 'get' working fine. 'post' causing me problems.
as see below 'pack up' json using json.stringify, fire off post rest resource. however, when object gets wcf method handling request object null.
here code:
$(document).ready(function () { var input = { customer: { customerid: "1", firstname: "luke", lastname: "sayaw", email: "lumsayaw@gmail.com", mobile: "0433395106", state: "qld" } }; $.ajax({ url: 'http://local.rest/restservice.svc/getcustomer', contenttype: "application/json; charset=utf-8", data: json.stringify(input), datatype: 'json', type: 'post', async: true, success: function (data, success, xhr) { alert('group saved - ' + data); alert('first name: ' + data.firstname); }, error: function (xhr, status, error) { alert('error! - ' + xhr.status + ' ' + error); } }); });
and server side codes:
namespace restservice { // note: can use "rename" command on "refactor" menu change class name "restservice" in code, svc , config file together. public class restservice : irestservice { public string getcustomer(customer customer) { string id = customer.customerid ; return new javascriptserializer ().serialize (customer ); } } [datacontract ] public class customer { public string customerid {get;set;} public string firstname { get; set; } public string lastname { get; set; } public string email { get; set; } public string mobile { get; set; } public string state { get; set; } } } namespace restservice { [servicecontract] public interface irestservice { [operationcontract] [webinvoke(method = "post", responseformat = webmessageformat.json, bodystyle = webmessagebodystyle.bare, uritemplate = "/getcustomer")] [return: messageparameter(name = "customer")] string getcustomer(customer customer); } }
many thanks
try:
[operationcontract, webget(uritemplate = "/getjson", bodystyle = webmessagebodystyle.bare)] //responseformat = webmessageformat.json stream getjson(); .... return new memorystream(encoding.utf8.getbytes(jsonstring));
Comments
Post a Comment