base_url in a .JS format



我有以下代码在我的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);
            } 
        });
    }));

我想加载ajax_upload_img_item.php,但它不会工作,因为我没有base_url();这是命令。我不能把它添加到脚本上。如何添加base_url();命令从Codeigniter配置,到script.js?谢谢你

在你的页面标题尝试这样做:

<header>
 <script type="text/javascript">
  var base_url = "<?= base_url() ?>";
 </script>
</header>
在你的。js文件中url变成了
url: base_url + "ajax_upload_img_item.php", // Url to which the request is sent

你需要这样做

<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>

在script.js文件

 url: BASE_URL+ "ajax_upload_img_item.php",

最新更新