以.JS格式的base_url

时间:2016-09-10 16:18:19

标签: javascript php ajax codeigniter

我在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的命令?谢谢

2 个答案:

答案 0 :(得分:3)

在页面的标题中尝试这样做: <报头GT;  < script type =" text / javascript">   var base_url ="<?= base_url()?>&#34 ;;  < /脚本> < /报头> 然后在你的.js文件中,url变为 url:base_url +" ajax_upload_img_item.php",//请求发送到的网址

答案 1 :(得分:1)

你需要这样做

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