当WCF具有(Ajax)对象参数时,我得到500(内部服务器错误)

时间:2015-12-31 14:51:46

标签: c# ajax wcf

我是Web Service和Ajax的新手。我正在尝试构建WCF应用程序(restful API)。目前,当我将js对象发送到WCF时,我遇到了一些问题。此外,当我向服务中的任何方法添加[WebInvoke(UriTemplate= "foo")]时,我在服务中的所有方法中都出错。

这是我的ajax

  $("#Insert").click(function () {
            var employee = {};
            employee.Id = "0";
            employee.Name = $("#nametext").val();
            employee.Nationality = $("#nationalitytext").val();
            employee.Email = $("#emailtext").val();

            $.ajax({
                url: '../GetEmployee.svc/insertEmpoyee',
                method: 'post',
                contentType: 'application/json;charset=utf-8',
                data: '{emp:' + JSON.stringify(employee) + '}',
                dataType: 'json',
                success: function () {

                },
                error: function (err) {
                    console.log('{emp:' + JSON.stringify(employee) + '}');
                    console.log(JSON.stringify(employee));
                }
            });

        });

这是我的WCF服务

 public void insertEmpoyee(Employee emp)
    {

        String strcon = ConfigurationManager.ConnectionStrings["DBSC"].ConnectionString;

        using (SqlConnection con = new SqlConnection(strcon))
        {
            SqlCommand cmd = new SqlCommand("ProInsertEmployeeData", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter() {
                ParameterName = "@Name",
                Value = emp.Name
            });

            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName = "@NameNationality",
                Value = emp.Nationality
            });

            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName = "@Email",
                Value = emp.Email
            });

            cmd.ExecuteNonQuery();
            con.Open();
        }
    }

这是服务合同

 [ServiceContract(Name = "../GetEmployee", Namespace = "http://localhost:51520")]
public interface IGetEmployee
{

    [OperationContract]
    [WebInvoke(UriTemplate= "insertEmpoyee")]
    Employee byId(int id);

    [OperationContract]
    List<Employee> getAll();

    [OperationContract]
    void insertEmpoyee(Employee emp);
}

这是Config

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="Getempbe">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="AjaxAndWCF.GetEmployeeAspNetAjaxBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<services>
  <service name="AjaxAndWCF.GetEmployee" behaviorConfiguration="Getempbe">
    <endpoint address="" behaviorConfiguration="AjaxAndWCF.GetEmployeeAspNetAjaxBehavior"
      binding="webHttpBinding" contract="AjaxAndWCF.IGetEmployee" >
    <identity>
      <dns value="localhost" />
    </identity>

    </endpoint>

  </service>
</services>

谢谢你们所有人。

0 个答案:

没有答案