Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I am new to WCF,i have created the ajax enabled wcf service but when i try to access it through Javascript i am not getting any response.

please help me


HTML code:
XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" language="javascript" src ="jquery.min.js">

    </script>


</head>
<body>
<form id="form1" runat="server">
<input id="Text1" type="text" />
<input id="Button1" type="button" value="Submit" onclick="HelloWorld()"/>
<script type="text/javascript">
    function HelloWorld() {
        var a = document.getElementById('Text1').value;
        
        $.ajax({
            type: "POST",
            url: "http://localhost:59191/Service1.svc/DoWork",
            data: "{'id':'" + a + "'}",
            contentType: "application/json; charset=utf-8 ",
            dataType: "json",
            success: OnSuccessCall,
            error: OnErrorCall
        });

    }

    function OnSuccessCall(response) {
        alert(response.d);
    }
    function OnErrorCall(response) {
        alert("Error");
    }

</script>

</form>
</body>
</html>



WCF Service:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace Wcftestthrajax
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";

        [OperationContract]
        [WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]
        public string DoWork(string id)
        {
            // Add your operation implementation here
            return "hello wcf" +id ;
        }

        // Add more operations here and mark them with [OperationContract]
    }
}


Webconfig:


XML
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="Wcftestthrajax.Service1AspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
<services>
<service name="Wcftestthrajax.Service1">
<endpoint address="" behaviorConfiguration="Wcftestthrajax.Service1AspNetAjaxBehavior"
                    binding="webHttpBinding" contract="Wcftestthrajax.Service1" />
</service>
</services>
</system.serviceModel>
</configuration>
Posted
Updated 12-May-13 23:33pm
v2
Comments
Yuriy Loginov 13-May-13 9:24am    
try changing your url to:

url: "http://localhost:59191/Service1/DoWork",
Chandan_Mysore 13-May-13 9:44am    
No its not working
Yuriy Loginov 13-May-13 10:25am    
put a breakpoint inside DoWork(string id) and see if the webservice gets called.

also the return type of DoWork is not a proper JSON string
Chandan_Mysore 13-May-13 23:54pm    
thanks for the reply..

no its no entering into WCF service, it is directly calling ajax callback [error] function
and one more thing if i try to run my wcfservice directly it displaying
"Metadata publishing for this service is currently disabled."

1 solution

Change Your Service annotations like below.Please make sure that your service is working or not
C#
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate ="/DoWork")]
 [OperationContract]


Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900