Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//When I run My Form Then Service Failed Function In Jquery Called!..
But I don't know where is the problem??
ASP.NET
//Code For Call WCF Service In WebForm
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>  
<script type="text/javascript"> 
    var Type;
        var Url;
        var Data;
        var ContentType;
        var DataType;               
        function CallService() {            
            $.ajax({
                type: Type, //GET or POST or PUT or DELETE verb
                url: Url, // Location of the service
                data: Data, //Data sent to server
                contentType: ContentType, // content type sent to server
                dataType: DataType, //Expected data format from server             
                success: function (msg) {//On Successfull service call
                    ServiceSucceeded(msg);
                },
                error: ServiceFailed// When Service call fails
            });
        }
        function ServiceFailed(result) {
            alert('Service call failed: ' + result.status + '' + result.statusText);
            Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;
        }
        function WCFJSON() {
            var id = "1";
            Type = "GET";
            Url = "http://localhost:52410/RestServiceImpl.svc/JSONData";
           Data = '{"id"= "' + id + '"}';
           // Data = {};
            ContentType = "application/json; charset=utf-8";
            DataType = "json"; 
            CallService();
        }
        function ServiceSucceeded(result) {

            if (DataType == "json") {
                resultObject = result.JSONDataResult;
                for (i = 0; i < resultObject.length; i++) {
                    alert(resultObject[i]);
                }
            }
        }      
        $(document).ready(
         function () {
             WCFJSON();
         }
         );        
        
    </script>   
//Code In Web Config
  <system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

//Code In Wcf Service...
//My InterFace IRestServiceImpl In Wcf Service!...
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped)
            ]
        string XMLData(string id);

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped)
           ]
        string JSONData(string id);
    }
}

//This Is InterFace Implementaion
    public class RestServiceImpl : IRestServiceImpl
    {      
        public string XMLData(string id)
        {
            return "You requested product " + id;
        }
        public string JSONData(string id)
        {
            return "You requested product " + id;
        }
Posted
Updated 9-Dec-12 22:18pm
v4

1 solution

I can see one problem in the code.
That is the starting script tag is missing.
Your whole JavaScript Code should be inside script tags, but there is no start tag of script.

Just above the code var Type;, you should have one script tag like <script>. You have one end tag for this, but no start tag.
Please add this.

Thanks...
 
Share this answer
 
Comments
RahulRana723 10-Dec-12 2:38am    
sir code is successfully running.my problem is why the servicefalied method is calling?...my error is 0--No Transport
Have you added that script start tag? Sometimes missing tags creates problem.
RahulRana723 10-Dec-12 4:17am    
yes sir.in my code script tag already added

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