asp.net - jQuery ajax POST error for wcf sevice -
this cross-domain ajax request:
$.ajax({ type: "post", datatype: "jsonp", contenttype: "application/jsonp", data: '{"username":"newuser","password":"pwd"}', crossdomain: true, async: false, url: "http://xxx.xx.xx.xx/myservice/sampleservice.svc/getdata", jsonpcallback: function (jsondata) { console.log(jsondata); alert('hi'); }, complete: function (request, textstatus) { alert(request.responsetext); alert(textstatus); }, error: function (request, textstatus, errorthrown) { alert(textstatus); } });
this wcf rest service's method:
namespace sampleservicerestapi { [aspnetcompatibilityrequirements(requirementsmode = aspnetcompatibilityrequirementsmode.allowed)] [servicebehavior(instancecontextmode = instancecontextmode.single)] public class sampleservice : isampleservice { ... public string getdata(userdata userdata) { string response = "hi_" + userdata.username; return response; } ... }
interface:
[webinvoke(method = "post", requestformat = webmessageformat.json, responseformat = webmessageformat.json, bodystyle = webmessagebodystyle.bare, uritemplate = "getdata")] [operationcontract] string getdata(userdata userdata);
datacontract:
[datacontract] public class userdata { [datamember] public string username { get; set; } [datamember] public string password { get; set; } }
and thepart of web.config
:
<system.web> <compilation debug="true" targetframework="4.0"/> <pages controlrenderingcompatibilityversion="3.5" clientidmode="autoid"/> </system.web> <system.servicemodel> <standardendpoints> <webscriptendpoint> <standardendpoint name="" crossdomainscriptaccessenabled="true"/> </webscriptendpoint> </standardendpoints> <servicehostingenvironment multiplesitebindingsenabled="true" aspnetcompatibilityenabled="true" > <serviceactivations> <add factory="system.servicemodel.activation.servicehostfactory" relativeaddress="sampleservice.svc" service="sampleservicerestapi.sampleservice"/> </serviceactivations> </servicehostingenvironment> <services> <service behaviorconfiguration="servicebehaviour" name="sampleservicerestapi.sampleservice"> <endpoint address="" behaviorconfiguration="web" binding="webhttpbinding" bindingconfiguration="webwinbinding" contract="sampleservicerestapi.isampleservice" /> </service> <!--<service behaviorconfiguration="metadatabehavior" name="myservice"> <endpoint address="mex" binding="mexhttpbinding" contract="imetadataexchange"/> </service>--> </services> <behaviors> <endpointbehaviors> <behavior name="web"> <webhttp/> </behavior> </endpointbehaviors> <servicebehaviors> <behavior name="servicebehaviour"> <servicemetadata httpgetenabled="true" /> <servicedebug includeexceptiondetailinfaults="true" /> </behavior> <behavior name=""> <servicemetadata httpgetenabled="true" /> <servicedebug includeexceptiondetailinfaults="true" /> </behavior> </servicebehaviors> </behaviors> <bindings> <webhttpbinding> <binding name="webwinbinding" maxbuffersize="2147483647" crossdomainscriptaccessenabled="true" maxreceivedmessagesize="2147483647"> <readerquotas maxarraylength="100000" maxstringcontentlength="2147483647" /> </binding> </webhttpbinding> </bindings> </system.servicemodel> <!--<system.web.extensions> <scripting> <webservices> <jsonserialization maxjsonlength="2147483644"/> <protocols> <add name="httppost"/> <add name="httppostlocalhost"/> </protocols> </webservices> </scripting> </system.web.extensions>--> <system.webserver> <modules runallmanagedmodulesforallrequests="true"/> <httpprotocol> <customheaders> <add name="access-control-allow-origin" value="*" /> <add name="access-control-allow-headers" value="content-type" /> </customheaders> </httpprotocol> </system.webserver> <appsettings> <add key="errorcodefile" value="~/app_data/errorcode.txt"/> </appsettings>
the error got in firebug
:
"networkerror: 500 internal server error - http://173.161.176.229/myservice/sampleservice.svc/getdata?callback=undefined&{%22username%22:%22newuser%22,%22password%22:%22pwd%22}&_=1369120080493"
the 500 error indicates somethings wrong on serverside. should examine going wrong there. possible have wcf part. perharps helps: http://msdn.microsoft.com/en-us/library/ms732023.aspx
Comments
Post a Comment