简体   繁体   中英

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:

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. I am not sure why it's happening. Am I doing something wrong with the jQuery Ajax Post Webservice call?

Say for example, my home page URL of the webapp is : https://example.com/home.html

After entering loging credentials, it gets added to the URL for some reason like this:

https://example.com/home.html?username=myname&password=mypassword

Here is my 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:

<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

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 :

 <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.

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM