在下面的代码中,alert为我提供了undefined。我认为问题是由于URL中的反斜杠或其他原因造成的。这是json结构。
"{
\"UserName\":\"abc\",
\"Password\":\"123456\"
}"如何验证用户输入的用户名和密码是否存在于URL中?
<form action="#" id="loginForm">
<div class="form-group">
<label for="userName" class="font-weight-bold" style="width:100%;">Username<a href="#" class="float-right font-weight-light axa-color">Forgot?</a></label>
<input type="text" class="form-control" id="userName" name="userName" placeholder="Enter Email">
</div>
<div class="form-group">
<label for="password" class="font-weight-bold" style="width:100%;">Password<a href="#" class="float-right font-weight-light axa-color">Forgot?</a></label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter Password">
</div>
<button type="button" id="login_btn" class="btn btn-lg btn-block">LOGIN</button>
</form>$("#login_btn").click(function() {
var userName = $("#userName").val();
var password = $("#password").val();
authenticate(userName, password);
});
function authenticate(userName, password1) {
$.ajax({
type: "POST"
url: "http://api/User/login",
dataType: 'json',
async: false,
data: {
username: userName,
password: password1
},
success: function(data) {
alert(data);
}
})
};发布于 2019-02-21 20:38:36
将数据更改为:
data: {
userName: userName,
password: password
},(您将它写在了错误的位置。)在此之后,您的未定义错误将消失。
在function()中,您还编写了password1而不是password。
发布于 2019-03-12 02:06:51
传递给$.ajax函数的对象中有一个拼写错误,在"POST"之后需要一个,
$("#login_btn").click(function() {
var userName = $("#userName").val();
var password = $("#password").val();
authenticate(userName, password);
});
function authenticate(userName, password1) {
$.ajax({
type: "POST",
url: "https://reqres.in/api/users",
dataType: 'json',
async: false,
data: {
username: userName,
password: password1
},
success: function(data) {
console.log(data);
}
});
};<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#" id="loginForm">
<div class="form-group">
<label for="userName" class="font-weight-bold" style="width:100%;">Username<a href="#" class="float-right font-weight-light axa-color">Forgot?</a></label>
<input type="text" class="form-control" id="userName" name="userName" placeholder="Enter Email">
</div>
<div class="form-group">
<label for="password" class="font-weight-bold" style="width:100%;">Password<a href="#" class="float-right font-weight-light axa-color">Forgot?</a></label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter Password">
</div>
<button type="button" id="login_btn" class="btn btn-lg btn-block">LOGIN</button>
</form>
https://stackoverflow.com/questions/54803581
复制相似问题