Doubt about ajax url

Asked

Viewed 590 times

0

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

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

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

$.ajax({
            url: '<?php echo base_url() ?>usuario/remover',
            type: 'POST',
            data: dados,

1 answer

1


In the first case (URL: url):

url is a previously defined variable in the code of your file . js where is the function you mentioned, 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 get the base URL. For example:

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

Will return http://seusite.com.br/blog/post/123

Just as:

echo base_url();

Will return http://seusite.com.br/

But please do not insert html code into . js files is extremely not recommended. If you need to do this, you can perform by echo the variables in the DOM and then pick up with 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 -->

Browser other questions tagged

You are not signed in. Login or sign up in order to post.