javascript - WCF data service not responding with json, only xml -
i trying consume data service function jquery ajax call, i've tried many ways of calling it, , setting service contract , data service, no matter keeps giving me xml. have heard people need use jsonp, necessary?
[servicecontract] public interface iservice1 { [operationcontract] [webinvoke(method = "post", bodystyle = webmessagebodystyle.wrappedrequest, responseformat = webmessageformat.json)] string getdata(int value); [operationcontract] compositetype getdatausingdatacontract(compositetype composite); // todo: add service operations here } // use data contract illustrated in sample below add composite types service operations. [datacontract] public class compositetype { bool boolvalue = true; string stringvalue = "hello "; [datamember] public bool boolvalue { { return boolvalue; } set { boolvalue = value; } } [datamember] public string stringvalue { { return stringvalue; } set { stringvalue = value; } } }
this data service class
public class myservice : dataservice<myentities> { private readonly myentities _datasource; public myservice() : this(new myentities()) { } // im doing di since testing service operations local db public myservice(myentities datasource) { _datasource = datasource; } protected override myentities createdatasource() { return _datasource; } // method called once initialize service-wide policies. public static void initializeservice(dataserviceconfiguration config) { config.setentitysetaccessrule("teams", entitysetrights.allread); config.dataservicebehavior.maxprotocolversion = dataserviceprotocolversion.v3; }
}
and jquery ajax. alerts error, , when inspect errors in console, json parse error because getting xml.
var url = "http://localhost:2884/myservice.svc/teams"; $.ajax({ type: "get", url: url, contenttype: 'application/json; charset=utf-8', accept: 'application/json', datatype: 'json', success: function (msg) { alert(msg.d); }, error: function(xhr, ajaxoptions, thrownerror) { alert("error : " + xhr + ajaxoptions + thrownerror); } });
if stuck @ point in wcf data services, easy assume getting started, was.
my solution problem, move away wcf, , web api (microsoft seems doing same) reference: http://www.codeproject.com/articles/341414/wcf-or-asp-net-web-apis-my-two-cents-on-the-subjec
plus how simpler things are, , able working within few minutes.
public class teamscontroller : apicontroller { team[] teams; // defined whatever persistant means u want. ie. entity framework. public ienumerable<team> getallteams() { return teams; } }
then js
$.getjson("api/teams/", function (data) { alert(data); });
yep better :d used tutorial http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
Comments
Post a Comment