网址嵌入两次

时间:2013-10-22 11:08:14

标签: javascript php jquery json codeigniter

我有以下脚本:

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$( document ).ready(function() {
$(function() {
   $('#upload_file').submit(function(e) {
      e.preventDefault();
      $.ajaxFileUpload({
         url         :'./upload/upload_file/', 
         secureuri      :false,
         fileElementId  :'userfile',
         dataType    : 'json',
         data        : {
            'title'           : $('#title').val()
         },
         success  : function (data, status)
         {
            if(data.status != 'error')
            {
               $('#files').html('<p>Reloading files...</p>');
               refresh_files();
               $('#title').val('');
            }
            alert(data.msg);
         }
      });
      return false;
   });
});
  setInterval(function refresh_files()
{
   $.get('./upload/files/')
   .success(function (data){
      $('#files').html(data);
   });
},5000
);

$('.delete_file_link').live('click', function(e) {
   e.preventDefault();
   if (confirm('Are you sure you want to delete this file?'))
   {
      var link = $(this);
      $.ajax({
         url         : '<?php echo base_url()?>/upload/delete_file/' + link.data('file_id'),
         dataType : 'json',
         success     : function (data)
         {
            files = $('#files');
            if (data.status === "success")
            {
               link.parents('li').fadeOut('fast', function() {
                  $(this).remove();
                  if (files.find('li').length == 0)
                  {
                     files.html('<p>No Files Uploaded</p>');
                  }
               });
            }
            else
            {
               alert(data.msg);
            }
         }
      });
   }
});
});

我的控制器中有以下功能:

public function upload_patients_details(){
       $id=$this->uri->segment(3);
       $sql = "SELECT CONCAT( fname, '', lname ) AS Patients_Name FROM patients
WHERE id = '$id' LIMIT 0 , 1";
       $result = $this->db->query($sql);
       $result1 = $result->result_array();
       foreach ($result1 as $key) {

           $patients_name = $key['Patients_Name'];

           $data['Patients_Name']=$patients_name;

       $data['dropdown_type']=$this->get_radiology_type();

      $this->load->view('upload',$data);
       }

   }
public function upload_file()
{
   $status = "";
   $msg = "";
   $file_element_name = 'userfile';

   if (empty($_POST['title']))
   {
      $status = "error";
      $msg = "Please enter a title";
   }

   if ($status != "error")
   {
      $config['upload_path'] = './radiology/';
      $config['allowed_types'] = 'gif|jpg|png|doc|txt';
      $config['max_size']  = 1024 * 8;
      $config['encrypt_name'] = TRUE;

      $this->load->library('upload', $config);

      if (!$this->upload->do_upload($file_element_name))
      {
         $status = 'error';
         $msg = $this->upload->display_errors('', '');
      }
      else
      {
         $data = $this->upload->data();
         $file_id = $this->files_model->insert_file($data['file_name'], $_POST['title']);
         if($file_id)
         {
            $status = "success";
            $msg = "File successfully uploaded";
         }
         else
         {
            unlink($data['full_path']);
            $status = "error";
            $msg = "Something went wrong when saving the file, please try again.";
         }
      }
      @unlink($_FILES[$file_element_name]);
   }
   echo json_encode(array('status' => $status, 'msg' => $msg));
}
 public function files()
{
   $files = $this->files_model->get_files();
   $this->load->view('files', array('files' => $files));
}
public function delete_file($file_id)
{
   if ($this->files_model->delete_file($file_id))
   {
      $status = 'success';
      $msg = 'File successfully deleted';
   }
   else
   {
      $status = 'error';
      $msg = 'Something went wrong when deleteing the file, please try again';
   }
   echo json_encode(array('status' => $status, 'msg' => $msg));
}

以下观点:

<!doctype html>
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
   <script src="<?php echo base_url()?>js/site.js"></script>
   <script src="<?php echo base_url()?>js/ajaxfileupload.js"></script>
   <link href="<?php echo base_url()?>css/upload.css" rel="stylesheet" />
</head>
<body>
   <h1>Upload File</h1>
   <form method="post" action="" id="upload_file">

       <?php //foreach($Patients_Name as $scan_types){?>
      <label for="title">Patient Name:</label>
      <input type="text" name="title" id="title" readonly value="<?php echo $Patients_Name?>" />


      <label for="userfile">File</label>
      <input type="file" name="userfile" id="userfile" size="20" />

      <label> Comment/Description:</label>
      <textarea rows="4" cols="20" id="comments" name="comments" placeholder="Please provide a brief description">  </textarea>


        <td> <p>
        <label for="scan_type">Scan Type <span class="required">*</span></label>
        <?php echo form_error('scan_type'); ?>




        <select  data-placeholder="Select a Scan Type..." class="scan_type"  name="scan_type" id="scan_type" >
                <option ></option>                          
                                     <?php foreach($dropdown_type as $scan_types){?>
                              <option  value="<?php echo $scan_types['dropdown_type']?>" id="<?php echo $scan_types['dropdown_type'] ?>" ><?php echo $scan_types['dropdown_type']?></option>
                              <?php } ?></select>

</p>
                </td>
      <input type="submit" name="submit" id="submit" />
   </form>
   <h2>Files</h2>
   <div id="files"></div>
</body>
</html

当我运行脚本:http://harrisdindi.com/caretech/upload/upload_patients_details/40时,我从firebug返回的脚本如下:GET http://harrisdindi.com/caretech/upload/upload_patients_details/upload/files/,主要构造函数正在加载两次,即从javascript上传,我该怎么办?解决这个问题?

1 个答案:

答案 0 :(得分:0)

我认为您的问题是由$.ajaxFileUpload配置中的相对网址引起的。 当您为url提供$.ajaxFileUpload密钥时,请尝试在javascript代码中使用绝对网址。

您可以使用php中的基本网址创建一个javascript变量,并在您的视图中显示以下内容:

// in <head> or somewhere near the top before any other <script> tag
<script>
    var BASE_URL = '<?php print base_url(); ?>';
</script>

现在您可以在$.ajaxFileUpload部分中使用此变量:

// ...
  $.ajaxFileUpload({
     url: window.BASE_URL+'/upload/upload_file/', 
// ....