javascript - .JS 格式的 base_url

标签 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 的命令?谢谢

最佳答案

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

<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

关于javascript - .JS 格式的 base_url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39428098/

相关文章:

javascript - 最安全的 JavaScript JSON 内联技术

javascript - jQuery动画函数计时

php - 检查用户名是否被占用

php - 登录脚本未按要求工作

javascript - 如何根据事件用不同的颜色突出显示日历中的日期?

javascript - 将焦点放在页面加载上的 asp.net 控件上

java - primefaces - 无 Activity 函数

PHP PDO - $dbh 和 $sth 代表什么?

javascript - 加载 php 文件 + 获取变量 + ajax 重新加载

javascript - 在 ajax 调用后再次运行 jquery 函数