Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 22nd, 2012  |  syntax: None  |  size: 1.27 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Error with Ajax-Enabled WCF Service (JSON) when consuming from jquery .ajax()
  2. [ServiceContract(Namespace = "http://localhost/")]
  3. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  4. public class Items
  5. {
  6.     [OperationContract]
  7.     [WebGet]
  8.     public string HelloWorld()
  9.     {
  10.         return "Hello World.";
  11.     }
  12. }
  13.        
  14. <services>
  15.   <service name="OService.ReadServices.Items">
  16.     <endpoint address="soap" binding="basicHttpBinding" contract="OService.ReadServices.Items"/>
  17.     <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="OService.ReadServices.Items"/>
  18.   </service>
  19. </services>
  20.        
  21. <endpointBehaviors>
  22.     <behavior name="jsonBehavior">
  23.       <enableWebScript />
  24.     </behavior>
  25.   </endpointBehaviors>
  26.        
  27. $(document).ready(function () {
  28.     $.ajax({
  29.         type: "POST",
  30.         contentType: "application/json; charset=utf-8",
  31.         url: "http://localhost/OService/ReadServices/Items.svc/json/HelloWorld",
  32.         data: "{}",
  33.         dataType: "json",
  34.         success: function (msg) {
  35.             alert("success: " + msg.d);
  36.         },
  37.         error: function (xhr, textStatus, errorThrown) {
  38.             alert("error: " + textStatus + " - " + errorThrown + " - " + xhr);
  39.         }
  40.     });
  41. });