无法从网络服务获取响应



这是我在WebService中的代码.cs

private void ResponseData(string strJSON)
{
    Context.Response.Clear();
    Context.Response.ContentType = "application/json";
    Context.Response.Flush();
    Context.Response.Write(strJSON);
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void login(string login, string password)
{
    try
    {
        objda.loginid = login;
        objda.pass = password;
        ds = objda.getadminbyusernamepass();
        if (ds.Tables[0].Rows.Count > 0)
        {
            ResponseData(@"[{""result"":""success""}]");
        }
        else
        {
            ResponseData(@"[{""result"":""failure""}]");
        }
    }
    catch (Exception ex)
    {
        ResponseData(@"[{""result"":""error"""+ex.ToString()+"}]");
    }
}

其中objda是通过存储过程访问数据的对象。

这是 aspx 页:

 <form runat="server" id="form1">
     <div>
         <input type="text" id="login" runat="server" /><br />
         <input type="text" runat="server" id="password"/>
         <input type="button" value="submit" onclick="sendrequest();" />
     </div>
 </form>
<script>
    function sendrequest() {
        var a = $("#login").val();
        var b = $("#password").val();
        console.log(rowID);
        $.ajax({
            url: "WebService.asmx/login",
            data: '{"login":"' + a+ '","password":"' + b+ '"}',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset-utf-8",
            success: function (data) {
                alert("success");
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('error: ' + xhr.status + ' ' + thrownError);
            }
        });
    }
</script>

我在按下按钮时收到错误 500 未定义。另外,与其显示alert='success',我还想访问结果的值并显示它。任何建议都会有所帮助。

登录(login,password(方法的编写方式,它说,参数可以使用查询字符串提供。但是在你的ajax调用中,你已经将数据构造为json,这是不正确的。尝试将方法签名更改为

    public class CustomerModel
    {
        public string login { get; set; }
        public string password { get; set; }
    }
    public void login(CustomerModel customerlogin)
    {
        // Code
    }

最新更新