Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nhayder's avatar
Level 13

Axios ajax call not working working on chrome but working safari

i'm struggling with this issue since a couple of hours now, on my app i'm requesting data from DB using axios ajax function.

this is the js function on my store.js store file

        async saveText({commit}, [id, text, name, wIndex]){

            await axios.put('/admin/designer/api/widget/text', {

                elem: text,

                elemName : name,

                elemId: id,

            })

            .then(function (response) {

                commit('UPDATE_W_ELEM', [name, wIndex, response.data]);

            })

            .catch(function (error) {

                Event.$emit('requestAlertDanger'); 

            });

        },

this is the route

Route::put('/widget/text', 'PageController@updatewidgettext');

this is what i have on the controller


    public function updatewidgettext(Request $request)
    {

        $request->validate([

            'elem' => 'required|string',

            'elemName' => 'required|string',

            'elemId' => 'required|integer'

        ]);

        $name = $request['elemName'];

        $elemId = $request['elemId'];

        $elemText = trim($request['elem']);

        $updateWidget = Widget::findOrFail($elemId);
        
        $updateWidget->$name = Purifier::clean($request['elem'], 'designer');

        $updateWidget->save();
        
        Cache::forget('page-'.$updateWidget->page_id); // front page cached file

        return $updateWidget->$name;
        
    }

the function is working perfectly on chrome and firefox but not on safari, I'm not sure why by i getting this error

{
    "message": "The given data was invalid.",
    "errors": {
        "elem": [
            " field elem required."
        ],
        "elemName": [
            " field elem name required."
        ],
        "elemId": [
            " field elem id required."
        ]
    }
}

it looks like axios is not being able to pass the data on safari properly, ????

is there is anything i can do to solve this issue????

Any idea ????

0 likes
48 replies
Pciranda's avatar

change from put to post / patch, I'm pretty sure safari has restrictions with put.

// Route
Route::patch(...);


// Call
axios.patch(...)

1 like
Sergiu17's avatar

I can't even understand your post title

1 like
nhayder's avatar
Level 13

what not to understand ???? same function above is working on chrome and but its giving issue on safari

nhayder's avatar
Level 13

i tried patch and post and get but still getting same error, as per example above it looks like the validation method is throwing exception because the required data values are empty.

if i tried this on chrome the function is working just fine and i can see the changes on the front end page.

Any further ideas on how to solve this issue ???

fylzero's avatar

@nhayder Your title says NOT working on Chrome, NOT working on Safari... basically just not working.

@sergiu17 is correct. This is very confusing.

24 likes
fylzero's avatar

@nhayder This HAS to be a cache busting issue. Are you using Laravel Mix to cache bust your javascript files?

If not, you should be. Otherwise your javascript is being cached in Safari and not updated.

To test this theory, open a private window in Safari and try again. If it works... you need to implement cachebusting.

https://laravel.com/docs/6.x/mix#versioning-and-cache-busting

24 likes
nhayder's avatar
Level 13

@fylzero yes i'm using laravel mix and it's not a caching issue, I cleared it almost every time i make a change in the code

fylzero's avatar

@nhayder Did you try a private window in Safari? Sometimes clearing the cache doesn't fully clear the cache in the way you would expect. The fact that Chrome has 3 different methods of clearing the cache is evidence of this.

24 likes
fylzero's avatar

@nhayder Just for the sake of argument... try this...

async saveText({commit}, [id, text, name, wIndex]){

            $ajaxUrl = '/admin/designer/api/widget/text' + '?nocache=' + new Date().getTime();

            await axios.put($ajaxUrl, {
24 likes
fylzero's avatar

@nhayder I also just read an article that says sometimes Safari won't work unless you add a slash at the end of your url...

Try this as well...

await axios.put('/admin/designer/api/widget/text/', {

Sounds silly but worth a try.

24 likes
fylzero's avatar

@nhayder Try hard coding the values... see if it passes...

            await axios.put('/admin/designer/api/widget/text', {

                elem: 'some text here',

                elemName : 'some name here',

                elemId: 3,

            })

This will at least tell you if the issue is on the passing side or the receiving side.

24 likes
nhayder's avatar
Level 13

@fylzero i will try doing all these and let you know, but it looks like axios on safari is submitting empty arrray??? so the validation on the controller method is throwing the error.

but the function is fully working on safari, This is really strange.

nhayder's avatar
Level 13

@fylzero i tried all of the solutions you recommended but none of them fixed the problem

fylzero's avatar

@nhayder try to completely simplify the function... see if this works. Just as a test...

saveText(){
 
   axios.put('/admin/designer/api/widget/text', 
    {
                elem: 'testing',
                elemName : 'testing',
                elemId: 1,
    });

},
24 likes
nhayder's avatar
Level 13

@fylzero by hardcoding the values like that

            await axios.put('/admin/designer/api/widget/text', {

                elem: 'some text here',

                elemName : 'some name here',

                elemId: 3,

            })

On the controller endpoint i'm checking if i'm receiving any data form axios before validation like this

    public function updatewidgettext(Request $request)
    {
    return $request; // <--- this

        $request->validate([

            'elem' => 'required|string',

            'elemName' => 'required|string',

            'elemId' => 'required|integer'

        ]);

        $name = $request['elemName'];

        $elemId = $request['elemId'];

        $elemText = trim($request['elem']);

        $updateWidget = Widget::findOrFail($elemId);
        
        $updateWidget->$name = Purifier::clean($request['elem'], 'designer');

        $updateWidget->save();
                
    }

And this is the result i'm getting when using safari


[ ] // empty 

so as a conclusion axios is not functioning as it should and the data are not being passed to laravel controller as we expected, But instead laravel is throwing validation error because no data has been received.

Nakov's avatar

@nhayder what about sharing your route file, as to me it sounds like you are not hitting this action at all. You are trying to return $request object, which will never be an []. If you did $request->all() then you might get [].

fylzero's avatar

@nakov He is claiming it is giving the validation error at the bottom of his original post. So it sounds like it is hitting the endpoint. Also claims it works on Chrome but not Safari.

At this point I'm guessing it is something about the way he has Axios installed? Maybe?

@nhayder What happens if you just use axios.post instead of using put? Make sure to change your route to accept post and stick with the simple test example.

If that works... I'd just make it a post for now... then do some bench testing with a fresh copy of Laravel to figure out what is off about your setup.

24 likes
nhayder's avatar
Level 13

@nakov please read the hall conversation the

return $request;

was temperary just to check if i'm getting any data from axios.

you can read my initial post for permanent code as i have it on my app

nhayder's avatar
Level 13

@fylzero i'm hitting that end point in chrome ?????

NOT SAFAR ???

the code is fully working on chrome and firefox

Nakov's avatar

@nhayder I've read the whole conversation.

@fylzero he said that using this:

public function updatewidgettext(Request $request)
    {
    return $request; // <--- this

the result is [] it cannot be true.

nhayder's avatar
Level 13

@nakov this line

return $request;

was removed from the code, it was just for testing.

it's not there anymore i was explaining how things work

fylzero's avatar

@nhayder That should be $request->all() as @nakov said... and what are you talking about!?!

I said...

Also claims it works on Chrome but not Safari.

You replied...

i'm hitting that end point in chrome ???? NOT SAFAR ??? the code is fully working on chrome and firefox

What the hell is different between these?

24 likes
nhayder's avatar
Level 13

@fylzero yes tried them all , patch, post, put, all of the as passing empty array on safari but they work as they should be on chrome

fylzero's avatar

@nhayder Try a post instead of put as I suggested and let me know the result.

saveText(){
 
   axios.post('/admin/designer/api/widget/text', 
    {
                elem: 'testing',
                elemName : 'testing',
                elemId: 1,
    });

},
return $request->all():
Route::post('/admin/designer/api/widget/text', 'YourController@yourMethod');
24 likes
Nakov's avatar

@nhayder I am not saying it still stays there, I am just saying the when using return $request; the response will NEVER BE [] it will be an instance of the Request object..

Now as you say it does not work.. Have you checked the Network tab in your Safari browser?

When you hit the route, inspect the Network tab, and make sure that the params are passed to the correct endpoint.

nhayder's avatar
Level 13

@nakov this is what is showing on the network tab when hitting the route


{
    "message": "The given data was invalid.",
    "errors": {
        "elem": [
            " field elem required."
        ],
        "elemName": [
            " field elem name required."
        ],
        "elemId": [
            " field elem id required."
        ]
    }
}

Next

Please or to participate in this conversation.