我正在处理简单的应用程序,我正在使用jquery ajax post方法从 default2.aspx 访问 Request.aspx 页面,如下所示:
js page:
a{
display: block;
background: blue;
padding: 10px 30px;
color: rgba(255, 255, 255, 0.5);
border-bottom: 2px solid gray;
margin-bottom: -1px;
}
a:hover{
color: white;
}
});
Default.aspx的:
$(document).ready(function () {
$("#login").click(function () {
var email = $("#email").val();
var password = $("#password").val();
$.ajax({
type: "POST",
url: "Request.aspx?Login=True",
data: "{'username': '" + email + "','password': '" + password + "'}",
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
var url = "http://google.com";
//$(location).attr('href', url);
}
});
});
Request.cs页面:
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/LoginCode.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="container">
<div class="main" id="loginform">
<label>
Email :</label>
<input type="text" name="demail" id="email" />
<label>
Password :</label>
<input type="password" name="password" id="password" />
<input type="button" name="login" id="login" value="Login" />
</div>
</div>
</div>
</form>
</body>
问题: 作为回应NUll即将到来,但我期待json。请让我知道我哪里出错了。
如果您需要更多信息,我会尽力提供。
提前致谢。
答案 0 :(得分:0)
使用Restful Webservices或Web Method从AJAX调用是个好主意。
但你仍然可以做到以下几点。
使用Response.Write()
而不是返回字符串。
使用此代码。
private void test()
{
var response = new ResponseResult();
var javaScriptSerializer = new JavaScriptSerializer();
response.Status = -1;
response.ErrorMessage = "";
response.Action = "UserProjects";
response.Data = "The Username or password you entered is not valid";
string result = javaScriptSerializer.Serialize(response);
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(result); //write json string to output
}