[英]jQuery Ajax Post keep adding username and password on the URL
I am using the Login Dialog as mentioned here on jQWidgets which I think is not the problem I am having and hence it shouldn't matter if someone has used it before or not for answering my question: 我正在使用jQWidgets上提到的“登录对话框”,我认为这不是我遇到的问题,因此,是否有人之前使用过它或是否用于回答我的问题都没有关系:
When testing the login functionality by putting login credentials, the username and password keep getting added on the URL of the page which I don't want. 通过放置登录凭据来测试登录功能时,用户名和密码会不断添加到不需要的页面的URL上。 I am not sure why it's happening. 我不确定为什么会这样。 Am I doing something wrong with the jQuery Ajax Post Webservice call? 我对jQuery Ajax Post Webservice调用做错了吗?
Say for example, my home page URL of the webapp is : https://example.com/home.html
举例来说,我的网络应用首页网址为: https://example.com/home.html
: https://example.com/home.html
After entering loging credentials, it gets added to the URL for some reason like this: 输入日志凭证后,由于某种原因,它会被添加到URL:
https://example.com/home.html?username=myname&password=mypassword
Here is my HTML: 这是我的HTML:
<!-- Login HTML Begins -->
<div id="wrap">
<div id="window" caption="Login">
<div>
<form >
<table>
<tr>
<td>Username:</td>
<td><input style="width: 150px;" type="text" name="user" id = "username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input style="width: 150px;" type="password" name="password" id = "password" /></td>
</tr>
<tr>
<td colspan="2" align="right" valign="bottom">
<input type="submit" id="submit" value="Login" />
</td>
</tr>
</table>
</form>
</div>
</div>
<!-- Login HTML ends -->
Here is my Javascript Code: 这是我的Javascript代码:
<script type="text/javascript">
$(document).ready(function () {
$('#window').jqxWindow({ theme: "shinyblack", width: 250, height: 130, isModal: true });
$('#submit').jqxButton({ theme: "shinyblack" });
var loginUrl = "https://example.com:8443/Webservice/loginCheck"
$( "#submit" ).click(function() {
var userName = $("#username").val();
var passWord = $("#password").val();
var ajaxRequest = jQuery.ajax({
//beforeSend: TODO: show spinner!
data: {
username: userName,
passWord: passWord
},
dataType: "json",
method: "POST",
url: loginUrl
})
.done(function (data_, textStatus_, jqXHR_) {
// Validate the web service and retrieve the status.
if (typeof (data_) === "undefined" || data_ === null) { alert("Invalid data returned from LoginCheck Web Service"); return false; }
if (isEmpty(data_.webservice_status) || isEmpty(data_.webservice_status.status)) { alert("Invalid Web Service Status for LoginCheck Webservice!"); return false; }
if (data_.webservice_status.status != "SUCCESS") { alert(data_.webservice_status.message);
return false; }
})
.fail(function (jqXHR_, textStatus_, errorThrown_) {
alert("Hitting the Fail function : Error in LoginCheck webservice: " + errorThrown_);
return false;
});
}
});
</script>
The default protocol used by forms are GET so you need to override it using POST protocol 表单使用的默认协议是GET,因此您需要使用POST协议覆盖它
so you need something like this: 所以你需要这样的东西:
<form action="url" method="post">
..
..
..
</form>
also the embedded click function you should prevent some default by putting this code : 还有嵌入式点击功能,您应该通过放置以下代码来防止某些默认设置:
$("#submit").click(function(e){
e.preventDefault();
<!-- your statement !>
...
})
also the butto type : 还有butto类型:
<button type="button" id="submit"></button>
or 要么
<input type="button" id="submit">
The way you've set it up, you're submitting the form data in the traditional way rather than via AJAX. 设置方式是以传统方式而不是通过AJAX提交表单数据的。
One option is to add: 一种选择是添加:
$('form').on('submit',function(event){
event.preventDefault();
});
(A common error is to try to prevent form submission in a click handler attached to the submit button. There are a number of ways to submit a form and the submit button is only one of them.) (一个常见的错误是试图阻止在提交按钮附带的单击处理程序中提交表单。有多种提交表单的方法,而提交按钮只是其中的一种。)
Another option is to just remove the form
element. 另一种选择是仅删除form
元素。
Your form may be sending a get request, because you haven't prevented the default functionality of a form button. 您的表单可能正在发送获取请求,因为您尚未阻止表单按钮的默认功能。 Try adding these two lines to your click handler: 尝试将这两行添加到点击处理程序中:
$( "#submit" ).click(function(event) {
event.preventDefault();
event.stopPropagation();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.