0

I am rendering my views in html and having my backend api in laravel. After the user logs in from cloudapp.azure.com/welcome.html, my script cloudapp.azure.com/js/script2.js sends the users password to my Laravel Controller;

//cloudapp.azure.com/js/script2.js

if(error_username == false && error_password == false){

        var details = [$("#username1").val(),$("#password1").val()];


        var url = "http://depressionapp1.westeurope.cloudapp.azure.com/index.php/";

        $.ajax({
           url: url,
           type: 'POST',
           data: {dataname: details},               
           success: function(result){

                 //THIS IS WHERE I AM HAVING PROBLEMS

                 location.href = "http://depressionapp1.westeurope.cloudapp.azure.com/welcome1.html";

                 /* or
                 var url1 =  "http://depressionapp1.westeurope.cloudapp.azure.com/" + result;
                 $.get(url1);*/


           }




        });

My Controlller from route url cloudapp.azure.com/index.php/ handles the request and returns a string 'welcome.html' as required.

//UserController@postLogin works fine and returns 'welcome.html'

      public function postLogin(Request $request){
   $result = DB::table('users')->select('user_id')->where('username', $request['username'])->get();
   $user = $result[0];
   $val = '';       
   if(Auth::attempt(['username' => $request['username'], 'password' => $request['password']])){

        $count = Answer::where('user_id', $user->user_id)->where('level', '<>', NULL)->count();
        if($count){
            return redirect()->route('checkLevel'); 
        }else{ 
            $val = 'welcome1.html';
        }   
   }
   $data = array('page' => $val, 'id' => $user->user_id);
   return response($val);      

}

Now because Laravel's url route processing this return of 'welcome.html' is cloudapp.azure.com/index.php/ it always seems to stay in this url and upon success on my ajax call, I want to redirect to a html page of cloudapp.azure.com/welcome1.html ie. 'cloudapp.azure.com' + result(wich is 'welcome.html').

I have tried following

$.ajax({
           url: url,
           type: 'POST',
           data: {dataname: details},               
           success: function(result){

                 //THIS IS WHERE I AM HAVING PROBLEMS

                 location.href = "http://depressionapp1.westeurope.cloudapp.azure.com/welcome1.html";

                 /* or
                 var url1 =  "http://depressionapp1.westeurope.cloudapp.azure.com/" + result;
                 $.get(url1);*/


           }

But I seem to be stuck on Laravel's route and I cannot redirect.

LCJ
  • 22,196
  • 67
  • 260
  • 418
Moses
  • 43
  • 1
  • 5
  • 1
    Did you try `window.location` http://stackoverflow.com/a/506004/1133306 – Jared Eitnier Aug 10 '16 at 17:34
  • yes as shown above; location.href = http://depressionapp1.westeurope.cloudapp.azure.com/welcome1.html"; it didnt work. – Moses Aug 10 '16 at 17:36
  • What is the contents of `result` in your `success` function? – Jared Eitnier Aug 10 '16 at 17:38
  • its a string 'welcome.html' as shown above. – Moses Aug 10 '16 at 18:14
  • I've had similar issues with redirection after getting response from Laravel. Some variation of `location` worked for me. That's why I asked if you had tried `window.location=` even though it's basically the same. See https://developer.mozilla.org/en-US/docs/Web/API/Window/location – Jared Eitnier Aug 10 '16 at 18:16
  • @Moses try `window.location = http://someurl.com/more/path` – bcr Aug 10 '16 at 18:30

1 Answers1

0

Yours ajax call sends post request with this data:

        data: {
            dataname: [
                $("#username1").val(),
                $("#password1").val()
            ]
        },

And in your controller you are grabbing different values

$request['username']

so your ajax data should look like this:

        data: {
            username: $("#username1").val(),
            password: $("#password1").val()
        },

fix this and then check browser console to find any other errors.

sebbz
  • 554
  • 3
  • 11
  • Guys, the controller works fine as stated above, i have handled the login fine, it all works! that is not my issue here, its when i return, the string'welcome.html' and im trying to redirect to a html view in javascript, thats my issue. many thanks in advance though. – Moses Aug 10 '16 at 18:17