karthik pandian

karthik pandian

  • NA
  • 2
  • 5.2k

Calling WCF rest services using jquery

Oct 15 2011 5:24 AM
  i got output when i called wcf rest(post method) service from jquery in local using sql `database`  . 
 
  but when i published my wcf service in server i can't get response from  server.can any `one` pls  help me to solve this problem here i will attach my code below
  and i have used ms sql with wcf rest services my client side code will be jquery.
 
  here my wcf interface service 
 
  [OperationContract]
 
  [WebInvoke(UriTemplate = "InsertScore", Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
  int InsertScore(string ename, int eno);
 
  this s my class service to call inter face( service1.svc)
 
  public class Service1 : IService1
  {
  string strConString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
  my method  for insertion
 
  public int InsertScore(string ename, int eno)
  {
  int resule;
 
  Debugger.Break();
  try
  {
 
 
  using (SqlConnection con = new SqlConnection(strConString))
  {
  using (SqlCommand cmd = new SqlCommand("insert into employee1 values ('" + ename + "'," + eno + ")", con))
  {
  con.Open();
  resule = cmd.ExecuteNonQuery();
  con.Close();
  return resule;
  }
 
  }
 
  }
  catch (Exception ex)
  {
  clsLog objLog = new clsLog(AppDomain.CurrentDomain.BaseDirectory + "Log\\log.txt", 0);
  objLog.Write(ex.ToString());
  //return -1;
  //return "error";
  return -1;
  }
  }
  this is my jquery function:
 
  $(".submit").click(function () {
  $.ajax({
  cache: false,
  async: true,
  type: "POST",
  dataType: "json",
  // 
  url: " http://test.gloriatech.in:601/Service1.svc/InsertScore",
  data: '{ "ename": "' + $("#Name").val() + '", "eno" : "' + $("#no").val() + '" }',
  contentType: "text/json;charset=utf-8",
  success: function (r) { alert("Successfully Registered!!!"); },
  error: function (e) { alert(e.statusText); }
  });
  });
  });
  html tag:
  <form id="form1" runat="server">
 
  <p>
 
  NAME-
 
  <input
  type="text"
  id="Name" /><br />
  <br/>
  NO  -
  <input
  type="text"
  id="no" /><br />
 
  </p>
  <a
  href="#"
  class="submit">
  Post
  </a>
  <input type="button" value="Get" id="btn" /><br />
 
 
  <div id="Con">
  </div>
 
  </form>
 
 
  finally i am attaching web config
  <?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <system.web>
      <compilation targetFramework="4.0" debug="true">
        <assemblies>
          <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
        </assemblies>
      </compilation>
  <httpRuntime maxRequestLength="2016384"/>
    </system.web>
   
 
 
  <system.serviceModel>
  <behaviors>
  <serviceBehaviors>
  <behavior name="sampledemo.Service1Behavior">
  <serviceMetadata httpGetEnabled="true" />
  <serviceDebug includeExceptionDetailInFaults="false" />
  </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
  <behavior name="JsonBehavior">
  <webHttp />
  </behavior>
  </endpointBehaviors>
  </behaviors>
  <services>
  <service behaviorConfiguration="sampledemo.Service1Behavior"
  name="sampledemo.Service1">
  <endpoint address="" behaviorConfiguration="JsonBehavior" binding="webHttpBinding"
  contract="sampledemo.IService1">
  <identity>
  <dns value="localhost" />
  </identity>
  </endpoint>
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
  </services>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" >
 
 
  </serviceHostingEnvironment>
  </system.serviceModel>
  <system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
 
 
 
  <connectionStrings>
 
  <!--<add name="karthiklinkEntities" connectionString="metadata=res://*/Data.Model1.csdl|res://*/Data.Model1.ssdl|res://*/Data.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=DDPRO9WIN7NX86\SQLEXPRESS;Initial Catalog=karthiklink;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />-->
 
  <!--<add name="karthiklinkEntities" connectionString="metadata=res://*/Data.Model1.csdl|res://*/Data.Model1.ssdl|res://*/Data.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\sqlexpress;Initial Catalog=karthiklink;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />-->
  <!--<add name="constring" connectionString="Data Source=.\sqlexpress; Initial Catalog=karthiklink; User Id=sa; Password=cdtech123$;"/>-->
  <add name="constring" connectionString="Data Source=DDPRO9WIN7NX86\SQLEXPRESS; Initial Catalog=karthiklink; Integrated Security=true;"/>
  </connectionStrings>
  </configuration>
 
 
 
 



Answers (1)