Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET WCF jQuery
//When I run My Form Then Service Failed Function In Jquery Called!..
But I don't know where is the problem??
//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 9-Dec-12 19:25pm
Edited 9-Dec-12 22:18pm
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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...
Comments
RahulRana723 at 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
Tadit Dash at 10-Dec-12 2:42am
   
Have you added that script start tag? Sometimes missing tags creates problem.
RahulRana723 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 359
1 Richard MacCutchan 320
2 OriginalGriff 240
3 Jared Sanow 145
4 Snehasish_Nandy 115
0 Sergey Alexandrovich Kryukov 5,969
1 OriginalGriff 5,005
2 CPallini 2,473
3 Richard MacCutchan 1,547
4 Abhinav S 1,507


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100