在我的asp.net网站中,我需要简单的WebMethod,它返回到jquery ajax:
{ "valid": true } or { "valid": false }
http://bootstrapvalidator.com/validators/remote/
为此我使用代码:
public class IsValid
{
public bool valid { get; set; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static IsValid ValidUser(string userName)
{
JavaScriptSerializer js = new JavaScriptSerializer();
var jsonData = new IsValid
{
valid = true
};
if (userName.IsEmpty())
{
// it's for other validator.
return jsonData;
}
User user = DbContext.Entity.Users.FirstOrDefault(c => c.UserName == userName.Trim());
if (user != null)
{
jsonData = new IsValid
{
valid = false
};
}
return jsonData;
}
但它在js函数
中的response.d.valid insted of response.valid中返回值xhr.then(function(response) {
dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true', response.message ? response.message : null);
});
如何更改代码以返回response.valid? 感谢。
答案 0 :(得分:0)
不幸的是,你不能调整Framework 3.5和plus服务,不能返回"。"。但是如果你对这样的编码做一些改动,你可以使用as response.valid。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string ValidUser(string userName)
{
JavaScriptSerializer js = new JavaScriptSerializer(null);
jsonData = new IsValid
{
valid = false
};
return js.Serialize(jsonData);
}
xhr.then(function(response) {
response = jQuery.parseJSON(response);
dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true', response.message ? response.message : null);
});