Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts

Saturday, April 16, 2011

Ajax SVC getting 404 error when using https

We're using AJAX WCF service for javascript calls.
All worked well while we were running under http, but when we went to production and switched to https, all of a sudden service wasn't workin. Following was happening:

http://mytest.com/test.svc/js - worked fine
https://mytest.com/test.svc - worked fine
https://mytest.com/test.svc/js - 404 error

Turned out we were missing binding configuration for https. This blogpost describes how to fix it (i'm attaching the XML fragment just in case the blog goes missing...)

<system.serviceModel>
<behaviors>    
 <endpointBehaviors>
  <behavior name="TestServiceAspNetAjaxBehavior">
   <enableWebScript />
  </behavior>
 </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
 <service name="TestService">
  <endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
   binding="webHttpBinding" bindingConfiguration="webBinding" contract="TestService" />
 </service>
</services>
 <bindings>
   <webHttpBinding>
     <binding name="webBinding">
       <security mode="Transport">
       </security>
     </binding>
   </webHttpBinding>
 </bindings>
</system.serviceModel>

Tuesday, March 15, 2011

Calling OperationContract in SVC using JavaScript

I was attempting to use WebMethod in a masterpage. Since .Net doesn't support static inheritance, I was out-of-luck. The obvious alternative was to use web-service to do the job. Since I was using .net 4.0, I was hoping to utilize the SVC and not ASMX.

Here's how you can make it work:

1) add "myservice1.svc" using the "Add New Item" -> "AJAX-enabled WCF-Service" menu. Notice that it adds section to your web.config and also "myservice1.cs" into your App_Code.

2) create your method in the "App_Code/myservice1.cs" (sample method below). Warning! If you're using Web Site project as me, don't put anything in the ServiceContract's Namespace!

[OperationContract]
public void SampleMethod1(int param1)
{
// your code here
}

3) Next step is to register this service in your ScriptManager (in my case, I have it on my master page):

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/myservice1.svc" />
</Services>
</asp:ScriptManager>


4) Finally JavaScript that will call the method:

myservice1.SampleMethod1(2);

It's really that simple - the ScriptManager makes the "myservice1" available and you just call the methods. Again, please note that there's no namespace.

Sunday, January 30, 2011

Refresh UpdatePanel using JavaScript

In case you'd like to refresh the content of UpdatePanel by javascript (e.g. after PageMethod call, etc.), use following syntax:

__doPostBack('<%=UpdatePanel1.ClientID %>', '');

Notice, that you have to use the ClientID of the control!