2014-02-25 44 views
1

我想在我的網站中使用ajaxFIleUpload。我沒有收到JavaScript發送給PHP的JSON數據。我被困在這一點上。文件上傳是好的,但我無法接收任何後期值。我jQuery的功能是PHP是不是從jQ​​uery接收JSON數據

$(function() { 

    $(document).on("submit", "#upload_file", function(e) { 

     e.preventDefault(); 

     $.ajaxFileUpload({ 
      url    :base_url + 'payments/uploadPaymentSlip/', 
      secureuri  :false, 
      fileElementId :'userfile', 
      type  : 'POST', 
      data: { paymentFormInputAmount: 'asdasd' }, 
      success : function (data, status) 
      { 
       if(data.status != 'error') 
       { 
        $('#files').html('<p>Reloading files...</p>'); 
        //refresh_files(); 
        $('#files').val(''); 
       } 
       alert(data.msg); 
      }, 
      dataType: 'json' 
     }); 

    }); 
}); 

我的PHP函數是

function uploadPaymentSlip() { 
    $status = ""; 
    $msg = ""; 
    $file_element_name = 'userfile'; 

    $status = "error"; 
    // checking whether json value shows in php or not 
    // $_POST['paymentFormInputAmount'] is also not working 
    $msg = $_POST['paymentFormInputAmount']; 

    if ($status != "error") { 
     $config['upload_path'] = realpath(APPPATH . '../uploads/paymentSlip'); 
     $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)); 
} 
+0

你是如何調用你的PHP函數'uploadPaymentSlip'? – putvande

+0

url:base_url +'payments/uploadPaymentSlip /',這是部分調用PHP函數。我認爲調用PHP函數是正確的,因爲除了獲取json值之外,上傳是成功的。 – atomaprchya

回答

1

type: 'json'必須type: 'POST'

,你應該加上:

contentType: 'json', 
dataType: 'json' 

作爲參數傳遞給$.ajaxFileUpload

contentType表示您發送的數據爲json

dataType意味着你期待的結果類型的json.

+0

我根據你的建議改變了。我越來越'虛假'而不是'asdasd' – atomaprchya

+0

你得到錯誤,因爲你在PHP腳本中期待數據爲'json'。你似乎並沒有使用'$ _POST'作爲'json'對象,所以我建議你刪除'contentType:'json''並將'data:{'paymentFormInputAmount':'asdasd'}'改爲'data :{paymentFormInputAmount:'asdasd'},' – alalp

+0

無法正常工作。仍然是虛假的。請我編輯的jquery部分。我根據你的建議改變了。 – atomaprchya