Wcf from JQuery -
i'm trying invoke wcf service jquery ajax. service usual hello:
web service:
[servicecontract] public interface ihelloservice { [operationcontract] [webinvoke(method = "post", bodystyle = webmessagebodystyle.wrapped, responseformat = webmessageformat.json)] string getmessage(string nome); } public class helloservice : ihelloservice { public string getmessage(string nome) { return "hello " + nome + "!"; } }
web config:
<system.servicemodel> <behaviors> <servicebehaviors> <behavior name=""> <servicemetadata httpgetenabled="true" /> <servicedebug includeexceptiondetailinfaults="false" /> </behavior> </servicebehaviors> </behaviors> <servicehostingenvironment aspnetcompatibilityenabled="true" multiplesitebindingsenabled="true" /> </system.servicemodel>
consumer:
<script> $('#button1').click(function () { $.ajax({ url: "http://localhost:2021/helloservice.svc/getmessage", type: "post", data: json.stringify($('#textbox1').val()), datatype: "json", contenttype: "application/json; charset=utf-8", success: function (data) { alert("ciao"); $('#label1').html('hello ' + data + '!'); }, error: function (xmlhttprequest, textstatus, errorthrown) { alert(textstatus); } }); }); </script>
web.config consumer:
<system.servicemodel> <behaviors> <endpointbehaviors> <behavior name="endpbehavior"> <webhttp/> </behavior> </endpointbehaviors> </behaviors> <client> <endpoint address="http://localhost:2021/helloservice.svc" binding="webhttpbinding" contract="helloservicereference.ihelloservice" behaviorconfiguration="endpbehavior" /> </client> </system.servicemodel>
it give me error. code inside "success" don't run. tryed asp.net consumer , go.
i found tutorial
http://www.encodedna.com/wcf/tutorial/call-wcf-service-from-jquery-ajax-json-aspdotnet.htm
it goes if consumer inside service project.
when try run web service visual studio web client empty. runs if delete endpoint web config, consumer don't go external consumer).
this web.config
<system.servicemodel> <services> <service name="books" behaviorconfiguration="myservicebehavior"> <endpoint address="" binding="webhttpbinding" behaviorconfiguration="webendpointbehavior" name="webendpoint" contract="ibooks"/> </service> </services> <behaviors> <servicebehaviors> <behavior name="myservicebehavior"> <servicemetadata httpgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="true"/> </behavior> </servicebehaviors> <endpointbehaviors> <behavior name="webendpointbehavior"> <webhttp /> </behavior> </endpointbehaviors> </behaviors> <servicehostingenvironment multiplesitebindingsenabled="true"/> </system.servicemodel>
Comments
Post a Comment