POST
요청 대신 어떻게 든 GET
트리거됩니다.
둘째, ajax 호출이 호출되지 않습니다.
나는 이것을 백 번했지만 내가 시도한 모든 것이 아무것도하지 않는 것 같습니다. 이건 내 잘못 일 수도 있지만 제발 도와주세요. 더 필요한 것이 있으면 물어보십시오. 문안 인사
$(document).ready(function(){
console.log("bro");
$("#login").click(function () {
console.log("hi");
var name = $('#uname').val();
var password = $('#password').val();
console.log(name);
console.log(password);
$.ajax({
url:"login-check.php",
data: {name:name, password:password},
dataType:"text",
method:"POST",
success:function(data){
if(data === "1" || data === "2"){
alert(data);
console.log("hello");
window.open('index.php','_self');
}
else{
}
}
});
});
});
HTML :
<!-- Form Code -->
<div class = "container">
<h2 style="text-align:center;">Please Fill the form to Sign up</h2>
<form class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2" for="name">Username:</label>
<div class="col-sm-10">
<input type = "username" class = "form-control" name = "uname" id = "uname" placeholder = "username" required autofocus>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type = "password" class = "form-control" name = "password" id = "password" placeholder = "password" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-primary" name = "login" id="login">Sign In</button>
</div>
</div>
</form>
Click here to clean <a href = "logout.php" tite = "Logout">Session.
</div>
버튼이 form
태그 안에 있고 type
속성 이 없기 때문에 제출 버튼 역할을하고 ajax가 호출되기 전에 양식을 제출합니다 (기본 양식 메소드는 GET입니다).
세부:
method
attribute are submitted using the GET
methodbutton
that is inside a form
tag that doesnt explicitly set the type
attribute will be set to type="submit"
html
, when you click the button, it submits your form using the GET method before your click handler is triggered to call the ajaxtype="button"
on the button (this is what I would do)e.preventDefault
at the top of your click handler to cancel the default behaviour of submitting the actual formform
태그 외부로 이동합니다 (성공없이 시도했다고 말했지만 이 jsFiddle을 보면 작동 하므로 충분히 멀리 이동 하지 않았다고 생각합니다)Terry가 주석에서 언급했듯이 다음 action
과 method
같이 양식에 및 속성 도 명시 적으로 설정해야합니다 .
<form action="login-check.php" method="post" class="form-horizontal">
이렇게하면 사용자가 javascript를 비활성화 한 경우에도 로그인이 작동하며 일반적으로 좋은 방법입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다