参数未从 Ajax 调用中填充(已尝试一切)
Parameter not populating from Ajax call (Tried everything)
大家好。
我有一个简单的 Ajax
电话。简单来说,它从密码字段中读取密码,将其填充到变量中,然后将其发送到我的 StrengthCheck 控制器。因此,一切都运行良好,ajax 调用进入 StregthCheck ActionResult 并且 ajax 成功部分被触发。但是,我的 StrengthCheck actionResult 中的参数 Password
不会填充从我的 ajax 调用发送的密码。我真的相信我已经尝试了所有方法,任何输入都将不胜感激,无论它是可能的解决方案还是调试的高级技巧。
非常感谢,希望这个问题尽可能简单。
脚本
<script>
$(document).ready(function () {
$("#Password").change(function () {
debugger
var password = $("#Password").val();
$.ajax({
url: '@Url.Action("StrengthCheck", "Login")',
data: { Password: password },
dataType:'json',
type: 'POST',
success: function (result) {
$("#PasswordStrength").val(result);
if (result.PasswordStrength != "Blank" && result) {
console.log(result);
var lblStatus = document.getElementById("PasswordStrength");
lblStatus.style.width = result + "%";
switch (result)
{
case "20":
$("#PasswordStrength").removeClass("progress-bar-success").addClass("progress-bar-warning");
break;
case "40":
$("#PasswordStrength").removeClass("progress-bar-success").addClass("progress-bar-warning");
break;
case "60":
$("#PasswordStrength").removeClass("progress-bar-warning").addClass("progress-bar-success");
break;
case "80":
$("#PasswordStrength").removeClass("progress-bar-warning").addClass("progress-bar-success");
break;
case "100":
$("#PasswordStrength").removeClass("progress-bar-warning").addClass("progress-bar-success");
break;
}
}
},
error: function (jqxhr, textStatus, errorThrown) {
console.log(jqxhr);
console.log(textStatus);
console.log(errorThrown);
},
async: true,
processData: false
});
})
});
</script>
登录控制器
[AllowAnonymous]
[HttpPost]
public JsonResult StrengthCheck(PhysiotherapyDAL.ViewModels.PasswordResetViewModel data)
{
#region password strength
PasswordScore passwordStrengthScore = PasswordAdvisor.CheckStrength(data.Password);
switch (passwordStrengthScore)
{
case PasswordScore.Blank:
data.PasswordStrength = 0;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.VeryWeak:
data.PasswordStrength = 20;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.Weak:
data.PasswordStrength = 40;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.Medium:
data.PasswordStrength = 60;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.Strong:
data.PasswordStrength = 80;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.VeryStrong:
data.PasswordStrength = 100;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
default:
break;
}
#endregion
data.PasswordStrength = 0;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
}
密码重置视图模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PhysiotherapyDAL.ViewModels
{
public class PasswordResetViewModel
{
[Required(ErrorMessage = "Password required")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Confirm Password")]
[Required(ErrorMessage = "Confirm password required")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "The two passwords do not match. Please ensure they match.")]
public string ConfirmPassword { get; set; }
public string ResetCode { get; set; }
public string UserID { get; set; }
public bool success { get; set; }
public int PasswordStrength { get; set; }
}
}
这是 chrome 调试的屏幕截图,显示了整个 ajax 调用中的数据填充
Screenshot of chrome debugging
HttpPost 可以从QueryString 和请求正文中接收参数。
默认情况下,基本类型在查询字符串中搜索,复杂类型被认为在正文中。
您的方法被定义为从查询字符串而不是正文中接收密码。这就是为什么它保持空白。
您有两种解决方法。
- 您更改 ajax 调用,使密码位于查询字符串中。
- 您更改接收复杂模型的方法并留下您的 ajax
public class Model {
public string Password {get; set;}
}
如果没有任何效果,则使用查询字符串进行测试,为此更改您的 url,如下所示
$(document).ready(function () {
$("#Password").change(function () {
debugger
var password = $("#Password").val();
$.ajax({
url: '/Login/StrengthCheck?Password='+password ,
data: {},
dataType:'json',
----
})
方法二
$(document).ready(function () {
$("#Password").change(function () {
debugger
var password = $("#Password").val();
var data={
"password" :password ,
}
$.ajax({
url: '/Login/StrengthCheck' ,
data:Json.Stringify(data),
dataType:'json',
----
})
模型活页夹:- https://www.sharpencode.com/article/MVC/model-binders
大家好。
我有一个简单的 Ajax
电话。简单来说,它从密码字段中读取密码,将其填充到变量中,然后将其发送到我的 StrengthCheck 控制器。因此,一切都运行良好,ajax 调用进入 StregthCheck ActionResult 并且 ajax 成功部分被触发。但是,我的 StrengthCheck actionResult 中的参数 Password
不会填充从我的 ajax 调用发送的密码。我真的相信我已经尝试了所有方法,任何输入都将不胜感激,无论它是可能的解决方案还是调试的高级技巧。
非常感谢,希望这个问题尽可能简单。
脚本
<script>
$(document).ready(function () {
$("#Password").change(function () {
debugger
var password = $("#Password").val();
$.ajax({
url: '@Url.Action("StrengthCheck", "Login")',
data: { Password: password },
dataType:'json',
type: 'POST',
success: function (result) {
$("#PasswordStrength").val(result);
if (result.PasswordStrength != "Blank" && result) {
console.log(result);
var lblStatus = document.getElementById("PasswordStrength");
lblStatus.style.width = result + "%";
switch (result)
{
case "20":
$("#PasswordStrength").removeClass("progress-bar-success").addClass("progress-bar-warning");
break;
case "40":
$("#PasswordStrength").removeClass("progress-bar-success").addClass("progress-bar-warning");
break;
case "60":
$("#PasswordStrength").removeClass("progress-bar-warning").addClass("progress-bar-success");
break;
case "80":
$("#PasswordStrength").removeClass("progress-bar-warning").addClass("progress-bar-success");
break;
case "100":
$("#PasswordStrength").removeClass("progress-bar-warning").addClass("progress-bar-success");
break;
}
}
},
error: function (jqxhr, textStatus, errorThrown) {
console.log(jqxhr);
console.log(textStatus);
console.log(errorThrown);
},
async: true,
processData: false
});
})
});
</script>
登录控制器
[AllowAnonymous]
[HttpPost]
public JsonResult StrengthCheck(PhysiotherapyDAL.ViewModels.PasswordResetViewModel data)
{
#region password strength
PasswordScore passwordStrengthScore = PasswordAdvisor.CheckStrength(data.Password);
switch (passwordStrengthScore)
{
case PasswordScore.Blank:
data.PasswordStrength = 0;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.VeryWeak:
data.PasswordStrength = 20;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.Weak:
data.PasswordStrength = 40;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.Medium:
data.PasswordStrength = 60;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.Strong:
data.PasswordStrength = 80;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
case PasswordScore.VeryStrong:
data.PasswordStrength = 100;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
default:
break;
}
#endregion
data.PasswordStrength = 0;
return Json(JsonConvert.SerializeObject(data.PasswordStrength), JsonRequestBehavior.AllowGet);
}
密码重置视图模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PhysiotherapyDAL.ViewModels
{
public class PasswordResetViewModel
{
[Required(ErrorMessage = "Password required")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Confirm Password")]
[Required(ErrorMessage = "Confirm password required")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "The two passwords do not match. Please ensure they match.")]
public string ConfirmPassword { get; set; }
public string ResetCode { get; set; }
public string UserID { get; set; }
public bool success { get; set; }
public int PasswordStrength { get; set; }
}
}
这是 chrome 调试的屏幕截图,显示了整个 ajax 调用中的数据填充 Screenshot of chrome debugging
HttpPost 可以从QueryString 和请求正文中接收参数。 默认情况下,基本类型在查询字符串中搜索,复杂类型被认为在正文中。
您的方法被定义为从查询字符串而不是正文中接收密码。这就是为什么它保持空白。 您有两种解决方法。
- 您更改 ajax 调用,使密码位于查询字符串中。
- 您更改接收复杂模型的方法并留下您的 ajax
public class Model {
public string Password {get; set;}
}
如果没有任何效果,则使用查询字符串进行测试,为此更改您的 url,如下所示
$(document).ready(function () {
$("#Password").change(function () {
debugger
var password = $("#Password").val();
$.ajax({
url: '/Login/StrengthCheck?Password='+password ,
data: {},
dataType:'json',
----
})
方法二
$(document).ready(function () {
$("#Password").change(function () {
debugger
var password = $("#Password").val();
var data={
"password" :password ,
}
$.ajax({
url: '/Login/StrengthCheck' ,
data:Json.Stringify(data),
dataType:'json',
----
})
模型活页夹:- https://www.sharpencode.com/article/MVC/model-binders