来自REST服务的400错误请求

时间:2017-04-20 17:23:49

标签: ajax rest wcf typescript service

我正在尝试通过使用AJAX将Json数据传递到操作合同URL来与REST服务进行交互,但我一直收到错误请求并且不明白为什么。 我正在使用Typescript进行编码,这是执行Ajax请求的方法:

AjaxLogin = (e: JQueryEventObject) : boolean =>
{
   let email = $("#login-email").val(); 
   let pwd = $("#login-pwd").val(); 

        $.ajax({
        cache: false, 
        type: "GET", 
        url: "http://localhost:57522/PizzaService.svc/Login", 
        data: '{"email":"' + email + '", "pwd":"' + pwd + '" }', 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: this.LoginSuccess, 
        error: this.LoginError
    }); 

    e.preventDefault(); 
    return false; 

}

REST服务中的操作合同:

  [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, 
        RequestFormat = WebMessageFormat.Json, 
        ResponseFormat = WebMessageFormat.Json, 
        UriTemplate = "/Login")]
        Entities.User LoginUser(string email, string pwd);

实现了C#方法:

 public User LoginUser(string email, string pwd)
    {
        SqlConnection db;
        SqlCommand cmd;
        SqlDataReader reader;
        db = new SqlConnection(WebConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);

        cmd = new SqlCommand("SELECT * FROM Customer WHERE customerEmail = @email AND customerPassword = @pwd",db);
        cmd.Parameters.AddWithValue("@email", email);
        cmd.Parameters.AddWithValue("@pwd", pwd);

        using (db)
        {
            db.Open();
            reader = cmd.ExecuteReader(); 

            if(reader.HasRows)
            {
                reader.Read();

                int id = (int)reader["Id"];
                string firstname = reader["customerFirstname"].ToString();
                string lastname = reader["customerLastname"].ToString();
                int houseno = (int)reader["customerHouseno"];
                string postcode = reader["customerPostcode"].ToString();
                string tel = reader["customerTel"].ToString();

                return new User(id, firstname, lastname, houseno, postcode, tel, email, pwd); 
            }
            else
            {
                return null; 
            }
        }
    }

谢谢,我真的很感激。

0 个答案:

没有答案