0

Browser Current URL is:

http://localhost/a_project/index.html

I have a Ajax code calling a PHP file who checks the login details and return a unique id if found in database else returns false. On Ajax success i want to create a URL and redirect the browser to that. I tried following code:

$.ajax({
  url: 'site_api/user_login.php',
  type: 'POST',
  processData: false,
  contentType: false,
  cache: false,
  data: formData,
  success: function(res) {
    if (res > 0) {
      var pathArray = window.location.pathname.split('/');
      window.location = pathArray[1] + "/my%20page.html?id=" + res;
    } else {
      $('#login_error').html('Invalid Credentials. Please try again.');
    }

  },
  error: function(xhr, ajaxOptions, thrownError) {
    alert(xhr.status);
  }
});

But it always result in following URL:

http://localhost/a_project/a_project/my%20page.html?id=20

Instead it should be routed to:

http://localhost/a_project/my%20page.html?id=20

I have a feeling that i am missing something but not sure what.

5
  • you're setting location to "a_project/...." - this is relative to the current location localhost/a_project ... hence the doubling of a_project - try window.location = "my%20page.html?id="+res; - no leading / Commented Jul 4, 2015 at 10:14
  • I already did, still no luck. It routes to localhost/my%20page.html?id=20 Commented Jul 4, 2015 at 10:16
  • 1
    in that case ... try "/" + pathArray[1] + "/my%20page.html?id="+res; - that should work Commented Jul 4, 2015 at 10:19
  • @JaromandaX What you suggested didn't work exactly but it did helped me in correcting the error. I changed my code to following and it worked fine: window.location.protocol + "//" + window.location.host + "/" + pathArray[1] + "/my%20page.html?id="+res Commented Jul 4, 2015 at 10:29
  • it's odd that yo need the host in some cases but not in others ... I was going to suggest window.location = "./my%20page.html?id="+res; as well Commented Jul 4, 2015 at 10:31

3 Answers 3

1

try this:

window.location = window.location.origin + '/a_project/my%20page.html?id=20' + res;
Sign up to request clarification or add additional context in comments.

Comments

0

Why do you need to generate the URL?

As you site root is http://localhost/a_project/, the pages index.html and my page.html are in the same directory. You can use relative path.

Just use "my page.html?id=" + res

And I would strongly recommend NOT to have spaces in URL.

$.ajax({
    url: 'site_api/user_login.php',
    type: 'POST',
    processData: false,
    contentType: false,
    cache: false,
    data: formData,
    success: function(res) {
        if (res > 0) {
            location.href = "my%20page.html?id=" + res;
        } else {
            $('#login_error').html('Invalid Credentials. Please try again.');
        }

    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
    }
});

Comments

0

Add A Base URL in the javascript then concatinate with the original Url

 var baseUrl="http://example.com/";
$.ajax({
            url: 'site_api/user_login.php',
            type: 'POST',
            processData: false,
            contentType: false,
            cache: false,
            data: formData,
            success: function(res)
            {
                if(res > 0){
                    var pathArray = window.location.pathname.split( '/' );
                    window.location =baseUrl+ pathArray[1] + "/my%20page.html?id="+res; //changes Here
                } else {
                    $('#login_error').html('Invalid Credentials. Please try again.');
                }

            },
            error: function(xhr, ajaxOptions, thrownError)
            {
                alert (xhr.status);
            }
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.