Doubts about url ajax

0

I'm developing a simple crud with ajax and jquery and I came across the following question ...

$('#btnDelete').unbid().click(function(){
        $.ajax({
            type: 'ajax',
            method: 'post',
            async: false,
            url: url,
        })
    })

Note that my url only has url: url, in another location there is a similar script but with a different url. What would be the difference?

$.ajax({
            url: '<?php echo base_url() ?>usuario/remover',
            type: 'POST',
            data: dados,
    
asked by anonymous 01.06.2018 / 00:29

1 answer

1

In the first case (URL: url):

url is a variable previously defined in the code of your .js file where the function you mentioned is, for example:

var url = 'http://seusite.com.br/api/getUser'

$('#btnDelete').unbid().click(function(){
        $.ajax({
            type: 'ajax',
            method: 'post',
            async: false,
            url: url,
        })
    })

In the second case (php):

You are using php to handle the base URL. For example:

echo base_url("blog/post/123");

Will return link

Like:

echo base_url();

Will return link

But please do not insert the html code inside .js files is extremely not recommended. If you need to do this, you can do it by echoing the variables in the DOM and then picking up the javascript.

<!-- snip -->
<div id="dom-target" style="display: none;">
    <?php 
        echo base_url();
    ?>
</div>
<script>
    var div = document.getElementById("dom-target");
    var myData = div.textContent;
</script>
<!-- snip -->
    
01.06.2018 / 09:19