Saurabh
Saurabh

Reputation: 674

Recreate URL using JavaScript

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.

Upvotes: 0

Views: 374

Answers (3)

allvideolectures
allvideolectures

Reputation: 145

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

Upvotes: 0

Shrinivas Shukla
Shrinivas Shukla

Reputation: 4453

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

Upvotes: 0

Super Hornet
Super Hornet

Reputation: 2907

try this:

window.location = window.location.origin + '/a_project/my%20page.html?id=20' + res;

Upvotes: 1

Related Questions