0

Today I'm trying to use jquery ajax in codeigniter. I got a problem. The ajax code was in js file which included at the bottom of view

<script scr="<?php echo base_url() ?>assets/js/ajax_main.js"></script>

And inside the ajax_main.js

$.ajax({
    type        : 'GET',
    url         : "<?php echo base_url() ?>akun_baru?selectedRow="+id_row, 
    encode      : true
})

.done(function(status) {
    alert(status)
});

When I write

alert("<?php echo base_url() ?>")

Sure it just shows <?php echo base_url() ?>

I just thinking that maybe I need to transfer the value of base_url() to be saved in javascript. But how?

2
  • 2
    This may help stackoverflow.com/questions/27420759/… Commented May 5, 2017 at 2:35
  • Yes, that's it! I just need to define it on the header. Thanks. Commented May 5, 2017 at 2:37

2 Answers 2

2

You can do like this:

<script>var base_url="<?php echo base_url() ?>";</script>
<script scr="<?php echo base_url() ?>assets/js/ajax_main.js"></script>

Then

alert(base_url)
Sign up to request clarification or add additional context in comments.

Comments

1
Add this javascript line in header.php on top

<script>var base_url="<?php echo base_url() ?>";</script>

and then link the javascript file

like this:

<script>var base_url="<?php echo base_url() ?>";</script>  <!-- on top -->
<script scr="<?php echo base_url() ?>/js/custom.js"></script> <!-- After link javascript file -->

And then go to custom.js file and check alert

alert(base_url);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.