C# Can';无法从webservice获得响应
这是我在WebService.cs中的代码C# Can';无法从webservice获得响应,c#,jquery,ajax,web-services,json.net,C#,Jquery,Ajax,Web Services,Json.net,这是我在WebService.cs中的代码 private void ResponseData(string strJSON) { Context.Response.Clear(); Context.Response.ContentType = "application/json"; Context.Response.Flush(); Context.Response.Write(strJSON); } [WebMethod] [ScriptMethod(Res
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>
函数sendrequest(){
var a=$(“#login”).val();
var b=$(“#密码”).val();
console.log(rowID);
$.ajax({
url:“WebService.asmx/login”,
数据:{“登录”:“+a+”,“密码”:“+b+”}”,
类型:“POST”,
数据类型:“json”,
contentType:“application/json;charset-utf-8”,
成功:功能(数据){
警惕(“成功”);
},
错误:函数(xhr、ajaxOptions、thrownError){
警报('error:'+xhr.status+''+thrownError);
}
});
}
按下按钮时出现错误500未定义。另外,我不想显示alert='success'
,而是想访问result的值并显示它。任何建议都会很有帮助。login(login,password)方法的编写方式是,可以使用查询字符串提供参数。但是在ajax调用中,您将数据构造为json,这是不正确的。尝试将方法签名更改为
public class CustomerModel
{
public string login { get; set; }
public string password { get; set; }
}
public void login(CustomerModel customerlogin)
{
// Code
}
请检查console中的“网络”选项卡并共享错误描述?尝试将[System.Web.Script.Services.ScriptService]添加到类中。这将允许从客户端调用您的方法。这是帮助我的演练@balaji marimuthu错误500(内部服务器(错误)类型=xhr启动器=jquery-1.4.1.min.js:130。@rexroxm似乎共享了不同的消息,应该在网络->响应中显示消息描述,如“参数名称无效/丢失”