user2284341
user2284341

Reputation: 671

Implementing an AJAX enabled WCF Service - call from JQuery not responding

I'm going through an example posted on MSDN which I can't get to work. I have an ASP Web Form project in VS2015 with an AJAX Enabled WCF Service. I've hit Cntrl-F5 which worked ok. I tried entering the URI in the browser and got the appropriate response. enter image description here

I then added an Ajax Enabled ScriptManager to Default.aspx design view and right clicked to the 'Properties' view. I added my sevice name:

enter image description here

Here's the service itself:

namespace CandidateTest.Service{
[ServiceContract(Namespace = "CandidateTest.Service")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class eThorService
{
    public string button;
    [WebGet()]
    [OperationContract]
    public void getUpdate()
    {
        _Default def = new _Default();
        Console.WriteLine("WCF service working");
        def.RenderData(button);
    }
}

}

Here's my jquery script that I'm trying to call the service from:

$(function () {
//while ($('#eThorButton').text != "Stop") {
if ($('#eThorButton').click) {
    alert("APC2");
$.ajax({

    url: 'http://localhost:49620/Service/eThorService.svc/getUpdate',
    method: 'post',
    dataType: 'json',
    success: function(){
        alert("success");
    },
    error: function (err) {
        alert(err);
    } 
})
//delay(1000);
alert("Here");
} 


});

The changes creating the service made to my web.config are default which I haven't changed:

<system.serviceModel>
<services>
  <service name="CandidateTest.Service.eThorService">
    <endpoint address="" behaviorConfiguration="CandidateTest.Service.eThorServiceAspNetAjaxBehavior"
      binding="webHttpBinding" contract="CandidateTest.Service.eThorService" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="CandidateTest.Service.eThorServiceAspNetAjaxBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />

End point is empty which might be an issue?

So essentially this is my first attempt at creating an AJAX enabled service in WCF and the script doesn't call the service. The alerts I added were to see if my script was consuming the service at all and it appears not to be.

I've followed the example on enter link description here which is for VS2012 but I'm hoping that's not the issue. What do I need to do to get my script to call the service?

EDIT

On inspecting the script in Chrome's dev tools, I get two errors - enter image description here

So clearly my script can't find my web service although it appears to be the correct URL.

EDIT I fixed the script reference error but my service call still returns a 404

Upvotes: 0

Views: 977

Answers (1)

Mike Cofoed
Mike Cofoed

Reputation: 151

It looks like your page doesn't have a reference to the jQuery javascript file.

Your ajax call is using the POST verb, but your service is set up with the WebGet() attribute, meaning you need to modify your ajax call to use GET as the verb:

$.ajax({

    url: 'http://localhost:49620/Service/eThorService.svc/getUpdate',
    method: 'get',
    dataType: 'json',
    success: function(){
        alert("success");
    },
    error: function (err) {
        alert(err);
    } 
})

Upvotes: 1

Related Questions