2

In my Laravel, Vue JS application, I'm getting this type of error after I transferred my application to our server.

Symfony\Component\HttpKernel\Exception\HttpException

I saw the same problem here but there's no answer. I hope this time there will be someone that can provide an answer or solution for this type of error.

Note:

  1. This error only happens if I tried to access the application from a server using other devices.
  2. Error occurs irregularly. If it doesn't show on first, sometimes shows-up after 2 or more ajax request.
  3. I'm using vForm for my ajax request.
  4. It works fine on my pc/localhost.
  5. It works fine as well when I tried to access my application inside my server. I mean, I tried to use the browser inside our server and it works with no problem.

PS.

  • I checked my Request Header and X-CSRF-TOKEN is there.
  • In my Request Payload _token is there as well.

Below are my codes:

VueJS Method:

UpdateDepartmentInfo(){

            this.departmentToUpdate._token = this.$root.csrf;

            this.requestUpdateDepartmentOnProgress = true;

            this.departmentToUpdate.put('department/'+this.departmentToUpdate.id)
                .then((response) => {              
                    this.requestUpdateDepartmentOnProgress = false;      
                    this.GetDepartmentList();
                    swal(
                        'Department Info. Saved!',
                        'Department information has been successfully updated to database!',
                        'success'
                    );
                })
                .catch((error) => {
                    this.requestUpdateDepartmentOnProgress = false;
                    if (error.response.status == 401) {
                        alert('User session has expired. Please login again.');
                        location.replace("/login");
                    }
                });
        }

Laravel Controller:

public function update(Request $request, $id)
    {

        // Check if the department object from model if it exists
        try{
            $department = Department::findOrFail($id);
        }catch(\Exception $e){
            return response(['error' => 'The department you want to edit can\'t be found in the database!' ], 400);
        }

        // check if it was just the status was sent 
        if($request['newStat'] != null){
            $department->status  = $request['newStat'];
        }else{

            $this->validate($request,[
                'name'      => 'required|string|max:191',
                'code'      => 'required|string|max:10',
            ]);

            $department->name           = $request['name'];
            $department->code           = $request['code'];
            $department->description    = $request['description'];
            $department->status         = $request['status'];       
        }

        $department->save();
        return ['message' => 'ok'];


    }

Thank you in advance!

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
JunDotz
  • 131
  • 1
  • 4
  • 10
  • Can you add the stacktrace ? – Clément Baconnier Sep 26 '18 at 08:09
  • what is in your `.env` APP_URL – Shailendra Gupta Sep 26 '18 at 08:15
  • @ShaielndraGupta - `APP_URL=http://localhost` – JunDotz Sep 26 '18 at 08:25
  • @cbaconnier - I cannot find error or the stracktrace in my `laravel.log` but in the response here it is: (wait i don't know how to attached screenshot) – JunDotz Sep 26 '18 at 08:26
  • @cbaconnier - I can't even pasted the trace response here cause it's too long. – JunDotz Sep 26 '18 at 08:40
  • You code seems fine, unless status, description or newStat are not the attended type. It will be very difficult to help you without the stacktrace or the content+headers of your working/broke requests. – Clément Baconnier Sep 26 '18 at 11:39
  • Is it possible that the `PHPSESSID` is causing the error? when I access the application in my localhost and browser inside server they both don't have this `PHPSESSID` only this `XSRF-TOKEN` and `myappname_session` and it works fine. How to remove this `PHPSESSID`? – JunDotz Sep 26 '18 at 18:57
  • @cbaconnier - i put [here](https://docs.google.com/document/d/1q6n2ZzZfqqrTDyIcjWelIBFYWkzPBKvvB90wJIQMvU0/edit?usp=sharing) the stacktrace. – JunDotz Sep 26 '18 at 19:25
  • What's the HTTP code in the response? 419 ? I suspect a csrf token expiration – Clément Baconnier Sep 27 '18 at 06:25
  • @cbaconnier - Yes it's 419. so do you know the reason why it expires? – JunDotz Sep 27 '18 at 07:23
  • By default it expire after 120 minutes for [security reasons](https://softwareengineering.stackexchange.com/a/140165). Since you're mostly using VueJS, I guess you aren't reloading the page while using the form. Here's a trick to update your csrf token: https://stackoverflow.com/questions/31449434/handling-expired-token-in-laravel – Clément Baconnier Sep 27 '18 at 08:26
  • I'm also experiencing the same issue here, tried the solutions but still not working, here is a link : https://buynow.liqstage.co.za/ – jtvdw Sep 30 '20 at 21:21

4 Answers4

0

in your .env file make changes like this

APP_URL=http://your_server_url

or you have to put thin in your index.php file

header("Access-Control-Allow-Origin: http://localhost");
Shailendra Gupta
  • 1,054
  • 6
  • 15
  • Tried both suggestions but still getting the error. I can't really seems to understand why I'm getting the error cause if i use the server browser it works fine. I just get the error if i tried to access it publicly. Isn't it because of the delay? – JunDotz Sep 26 '18 at 09:25
  • updated the answer please check and let me know if not works – Shailendra Gupta Sep 26 '18 at 09:38
  • already put `header("Access-Control-Allow-Origin: http://localhost");` at the very top inside `public\index.php` but unfortunately the error is still there. :( – JunDotz Sep 26 '18 at 10:00
0

check in the resources/js/components folder in you component if you have
http://127.0.0.1:8000/fetchusers

change it to

 fetch('fetchusers',{
            method:'POST',
            headers:{
                'Content-Type':'application/json',
                X_CSRF_TOKEN:('meta[name="csrf-token"]').content,
                'Accept':'application/json'
            }
        })

it might help.

Mebatsion Sahle
  • 409
  • 2
  • 9
0

it's because of the csrf_token usually, you can check this documentation hope it will solve yours too.

https://laravel.com/docs/5.6/csrf#csrf-x-csrf-token

To do: <meta name="csrf-token" content="{{ csrf_token() }}">

    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});```
 
-1

check your storage/log/laravel.log file is there. Also they have permission to write/read

this can be fixed quite easily by running:

sudo chmod -R 777 storage/logs
Usman Jdn
  • 807
  • 8
  • 21