A
A
Alexander Maslov2016-01-20 21:57:57
JavaScript
Alexander Maslov, 2016-01-20 21:57:57

How can I prevent a page from being displayed that is not loaded via ajax?

Good day!

Maybe the question in the header is not quite correct, but I will try to describe the problem!

There is a page template on laravel blade
In which dynamic content is inserted via ajax. (html + data received from the controller)
Naturally, the content has no styles or other information (for example: forms, tables, etc.).
there are pages for editing user data /account/update. at this address you can get a piece of html with some data.

Through ajax (code below) I load data into the main page template

$(document).ready(function() {
    $('#menu a').click(function() {
        var url = $(this).attr('href');

        $.ajax({
            url:     url + '?link=1',
            success: function(r){
                $('#content').html(r);
            }
        });

        if (url != window.location){
            window.history.pushState(null, null, url);
        }

        return false;
    });

    $(window).bind('popstate', function() {
        $.ajax({
            url:     location.pathname + '?link=1',
            success: function(r){
                $('#content').html(r);
            }
        });
    });
});

Everything works successfully.
But the trouble is that if you just go to the address site.ru/account/updatewe get html code, without styles and everything else. Even if we redirect to this page after updating the data, we get the usual piece of code.
So that's what I'm all about, how to make sure that this doesn't happen, so that everything is loaded with ajax, but it's not in open form. Or maybe I'm completely driven and I'm doing everything wrong? (Help with advice please

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rikcon, 2016-01-20
@MrRino

if(Request::ajax()){
        return "AJAX";
    }
    return "HTTP";

G
Gregory, 2016-01-21
@difiso

You can do as Rikcon says , or you can do Middleware. The difference is how many such places you have: if there are many such places, then it is better to use middleware, if one or two - if(Request::ajax()).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question