简体   繁体   English

无法从网络服务获得响应

[英]Can't get response from webservice

This is my code in WebService.cs 这是我在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()+"}]");
    }
}

where objda is an object that accesses data through the a stored procedure. 其中objda是通过存储过程访问数据的对象。

This is the aspx page: 这是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>

I get error 500 undefined on button press. 按下按钮时出现未定义的错误500。 Also instead of showing alert='success' , I would like to access the value of result and show it. 另外,我不想显示alert='success' ,而是要访问result的值并显示它。 Any suggestions will be helpful. 任何建议都会有所帮助。

The way login(login,password) method is written, it says, the arguments can be supplied by using query string. 表示login(login,password)方法的方式,它说,可以使用查询字符串来提供参数。 But in your ajax call, you have constructed the data to be json which is not correct. 但是在您的ajax调用中,您已经将数据构造为json,这是不正确的。 Try changing your method signature to 尝试将您的方法签名更改为

    public class CustomerModel
    {
        public string login { get; set; }
        public string password { get; set; }
    }

    public void login(CustomerModel customerlogin)
    {
        // Code
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM