简体   繁体   English

.JS格式的base_url

[英]base_url in a .JS format

I have this code in my script.js: 我的script.js中有以下代码:

    $(document).ready(function (e) {
        $("#uploadimage").on('submit',(function(e) {
        e.preventDefault();
        $("#message").empty();
        $('#loading').show();
        $.ajax({
            url: "ajax_upload_img_item.php", // Url to which the request is send
            type: "POST",             // Type of request to be send, called as                                                 method
            data: new FormData(this), // Data sent to server, a set of key/value         pairs (i.e. form fields and values)
            contentType: false,       // The content type used when sending data to         the server.
            cache: false,             // To unable request pages to be cached
            processData:false,        // To send DOMDocument or non processed data         file it is set to false
            success: function(data)   // A function to be called if request succeeds
            {    
                $('#loading').hide();
                $("#message").html(data);
            } 
        });
    }));

I want to load the ajax_upload_img_item.php BUT it wont work because i dont have the base_url(); 我想加载ajax_upload_img_item.php但它无法正常工作,因为我没有base_url();。 command on it. 命令。 And i cant add it on the script. 我不能在脚本上添加它。 How will I add the base_url(); 我将如何添加base_url(); command from the Codeigniter config, to the script.js? 命令从Codeigniter配置到script.js? thank you 谢谢

in the header of your page try to do this: 在页面标题中尝试执行以下操作:

<header>
 <script type="text/javascript">
  var base_url = "<?= base_url() ?>";
 </script>
</header>

Then in your .js file url becomes 然后在您的.js文件中,URL变为

url: base_url + "ajax_upload_img_item.php", // Url to which the request is sent

You need to do it like this 你需要这样

<header>
 <script type="text/javascript">
  var BASE_URL= "<?php echo base_url() ?>";
 </script>
<script type="text/javascript" src="<?php echo base_url();?>your_resource_folder_path/script.js"></script>
</header>

In script.js file 在script.js文件中

 url: BASE_URL+ "ajax_upload_img_item.php",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM