我在ASP.NET WebForms应用程序中构建了一个简单的Bootstrap登录表单,如下所示:
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
$(document).ready(function () {
$('#signInBtn').click( function () {
if ($('#MemberName').val() == "" || $('#Password').val() == "") {
e.preventDefault();
return false;
}
var $btn = $(this);
$btn.button('loading');
var formData = {};
formData['MemberName'] = $('#MemberName').val();
formData['Password'] = $('#Password').val();
var json = JSON.stringify({ Member: formData });
$.ajax({
url: "members.asmx/ValidateLogin",
timeout: 30000,
type: 'POST',
data: json,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
var response = JSON.parse(data.d);
alert(response);
},
error: function (data, textStatus, jqXHR) {
var obj = jQuery.parseJSON(jqXHR.responseText);
alert(obj);
}
})
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMain" Runat="Server">
<div id="frm">
<div class="form-inline form-group-sm">
<div class="input-group">
<label for="MemberName" class="sr-only">Email Address :</label>
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="email" required id="MemberName" placeholder="Email Address" class="form-control">
</div>
<div class="input-group">
<label for="Password" class="sr-only">Password :</label>
<input type="password" required id="Password" placeholder="Password" class="form-control">
</div>
<button id="signInBtn" class="btn-sm btn-primary" autocomplete="off" data-loading-text="Wait...">Login</button>
</div>
</div>
</asp:Content>
它使用以下代码连接到Web服务:
using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using MCV.Data;
[WebService(Namespace = "http:/myproject.temp.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Members : System.Web.Services.WebService
{
public Members()
{ }
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ValidateLogin(Member NewMember)
{
string name = NewMember.MemberName;
SignInResponse r;
try
{
bool status = MemberDB.SignIn(NewMember.MemberName, NewMember.Password);
if (status)
r = new SignInResponse("1", "OK");
else
r = new SignInResponse("0", "ERR");
}
catch (Exception ex)
{
r = new SignInResponse("-1", ex.Message);
}
return JsonConvert.SerializeObject(r);
}
}
这不是复杂的代码。 Web服务只是调用另一个类中的方法来验证用户的电子邮件和密码,并返回包含自定义类的JSON字符串以及验证结果。
问题是,代码无声地失败。它没有出现失败,因为Chrome控制台没有显示任何错误。我在Web服务代码中添加了一个断点来查看它正在做什么,但是断点永远不会被命中。我不知道错误在哪里,但它让我感到厌恶。
对此有何想法?
答案 0 :(得分:1)
两个问题:
NewMember
$.ajax
参数
members.asmx/ValidateLogin
的javascript中,但在其背后的C#代码中,使用大写字母M称为Members
。确保案例匹配。 我刚刚进行了更改,以便从javascript正确构建NewMember
,并确保我的解决方案中的.asmx文件名为members.asmx
并且它现在正在运行:
$('#signInBtn').click(function () {
if ($('#MemberName').val() == "" || $('#Password').val() == "") {
e.preventDefault();
return false;
}
var json = {
"MemberName": $('#MemberName').val(),
"Password": $('#Password').val()
};
$.ajax({
url: "members.asmx/ValidateLogin",
timeout: 30000,
type: 'POST',
data: JSON.stringify({ NewMember: json }),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
var response = JSON.parse(data.d);
alert(response);
},
error: function (data, textStatus, jqXHR) {
var obj = jQuery.parseJSON(jqXHR.responseText);
alert(obj);
}
})
});