pavlen's avatar

Get base url for Javascript code

Hi, this is Js code and work fine:

<script>

  $("#form").submit(function (e) {
        e.preventDefault();    
        $.ajax({
         
            type : "POST",
            url : "http://dev.admin.com/charpost",
            dataType : "JSON",
            data : {
                'name' : $('input[name=name]').val(),
                "_token" : $('input[name=_token]').val()
            },
            success : function(response) {
            $('#my_div').html(response.message);
            setTimeout("location.href = 'http://dev.admin.com/characteristics'",3000); 
            }
        }); 
 });
</script>

so,this part is interesting for me :

setTimeout("location.href = 'http://dev.admin.com/characteristics'",3000);

Is there way to use some base url or some shorthand, I think this is ugly in code..

Tnx

0 likes
6 replies
pmall's avatar

Am I wrong or you are trying to post a form with an ajax request then redirect the page ? What the point ?

pmall's avatar

Again, what the point of making an ajax request if you refresh the page when it succeed...

pavlen's avatar

There are 3 forms on same page,so I need to refresh page to show all new contetn...

But that si not important now :)

I am just asking is this way to write route Ok or there is something specific for Laravel? :)

pmall's avatar

There are 3 forms on same page,so I need to refresh page to show all new contetn...

So do a normal http request. No need for ajax if you refresh the page.

monsterdream's avatar

I think you can add on beginning of your script something like that:

var newLocation = { { url('somewhere'); } }

And later, use it as normal variable.

Please or to participate in this conversation.