JQuery attempt to change window.location incorrectly truncating query
parameters
I'm experimenting with JQuery for fun and I've run into a bit of a brick
wall. I'm attempting to write a login page and my thought was to have a
form on a jsp where a user would enter a login id and password and click a
submit button. This then gets caught by a JQuery method that then makes an
AJAX call to the back-end. The "success" method should then set the window
object's location to a new URL indicated by the value returned from the
Ajax call. FYI: I'm a back-end guy and I have that portion covered. My
problem is that although I call the back-end and get the data I require. I
then attempt to set a new window location with a URL that contains a
single query parameter (to allow the app to know who the user is), which
almost works as I get to the base URL but with no query parameters (though
there is a '?' on the end of the base URL)
Here's my JQuery code:
<script>
$(document).ready(function(){
$("#loginForm").submit( function() {
var id = $("#login_id").val();
var pwd = $("#password").val();
var uri = "http://localhost:8080/SW_Server/rest/admin/login";
alert("uri: "+uri);
$.ajax({
async: false,
url: uri,
type: 'POST',
dataType: "json",
data: { "id":id, "pwd":pwd },
success: function(data) {
alert("data.redirect="+data.redirect);
$('#loginForm').attr('action',data.redirect);
alert($("#loginForm").attr("action"));
// Prevent the form's default submission.
event.preventDefault();
// Prevent event from bubbling up DOM tree, prohibiting
delegation
event.stopPropagation();
window.location.replace(data.redirect);
},
error: function(jqHHR, textStatus, errorThrown) {
alert("textStatus: "+textStatus+"\nerror: "+errorThrown);
// Prevent the form's default submission.
event.preventDefault();
// Prevent event from bubbling up DOM tree, prohibiting
delegation
event.stopPropagation();
}
});
});
</script>
When I execute this, my back end currently returns
"http://localhost:8080/test.jsp?session_id=3", which I see in the alert
(hooray for me), but then when window.location.replace(data.redirect) is
called it goes to "http://localhost:8080/test.jsp?" with no query
parameter. I've been banging my head all day. What can I do to get the
query param correctly picked up by the new page?
Regards,
Tim
No comments:
Post a Comment