Welcome Guest, Not a member yet? Register   Sign In
[Solved] Setting url in a .js file
#1

(This post was last modified: 11-02-2016, 03:21 AM by wolfgang1983.)

I have set my url in a js file


Code:
assets

assets > jquery > common.js



And I have a controller called Filemanager.php


Code:
application

application > controllers > Filemanager.php


When I try click on my button which trys to launch modal. It gives the wrong location and a 404 error


The url out puts

Code:
http://localhost/project-1/question/filemanager

How ever should only produce

Code:
http://localhost/project-1/filemanager


assets > jquery > common.js


Code:
$(document).ready(function() {
    $(document).delegate('img[data-toggle=\'image\']', 'click', function() {
        $('#modal-image').remove();
        $.ajax({
            url: 'filemanager',
            dataType: 'html',
            success: function(html) {
                $('body').append('<div id="modal-image" class="modal">' + html + '</div>');
    
                $('#modal-image').modal('show');
            }
        });
    });    
});   


Routes.php


Code:
$route['filemanager'] = 'filemanager/index';


Any idea in how to make sure it gets the right url I cannot use base_url in a js file.

Attached Files Thumbnail(s)
   
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

As a work around I have a view that is use in every pages.
The view is to set default styles and Javascript for the site. This include  a JavaScript variable for the site URL

PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url().index_page();?>";
</
script

Then use the variable to make the URL
Code:
$(document).ready(function() {
    $(document).delegate('img[data-toggle=\'image\']', 'click', function() {
        $('#modal-image').remove();
        $.ajax({
            url: BASE_URL + '/filemanger', // full URL
            dataType: 'html',
            success: function(html) {
                $('body').append('<div id="modal-image" class="modal">' + html + '</div>');
    
                $('#modal-image').modal('show');
            }
        });
    });    
});  

I hope this helps.
A good decision is based on knowledge and not on numbers. - Plato

Reply
#3

(This post was last modified: 11-02-2016, 03:17 AM by wolfgang1983.)

(11-01-2016, 10:45 PM)salain Wrote: As a work around I have a view that is use in every pages.
The view is to set default styles and Javascript for the site. This include  a JavaScript variable for the site URL

PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url().index_page();?>";
</
script

Then use the variable to make the URL
Code:
$(document).ready(function() {
    $(document).delegate('img[data-toggle=\'image\']', 'click', function() {
        $('#modal-image').remove();
        $.ajax({
            url: BASE_URL + '/filemanger', // full URL
            dataType: 'html',
            success: function(html) {
                $('body').append('<div id="modal-image" class="modal">' + html + '</div>');
    
                $('#modal-image').modal('show');
            }
        });
    });    
});  

I hope this helps.

I can access the url in browser but when use ajax says network error even though url is correct now.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#4

I never had any issues but I have no route setup for url I use with ajax.
Maybe ajax by pass the route, but that being said it should work as the index method is default.

Sorry, I don't know what could be the problem.
A good decision is based on knowledge and not on numbers. - Plato

Reply
#5

PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url();?>";
</
script
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 11-02-2016, 03:22 AM by wolfgang1983.)

(11-02-2016, 03:07 AM)InsiteFX Wrote:
PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url();?>";
</
script

Your one worked plus on common.js miss spelt also filemanager so both now are fixed.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#7

(11-02-2016, 03:20 AM)wolfgang1983 Wrote:
(11-02-2016, 03:07 AM)InsiteFX Wrote:
PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url();?>";
</
script

Your one worked plus on common.js miss spelt also filemanager so both now are fixed.


You can also do the same using site_url if you need the index.php
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB