2016-08-16 92 views
-2

我在發送數據時遇到了ajax問題。 Ajax返回ParseError我不知道爲什麼。任何幫助,將不勝感激。AJAX返回ParseError

function redeem_all_pts(co_id) { 
    var path = base_url+"cmaster/redeem_all"; 

    $.ajax({ 
     type:'POST', 
     url:path, 
     data:'co_id='+co_id, 
     dataType:'json', 
     success: function(resp) { 
      console.log("success!!="+resp); 
     }, 
     error:function(resp, error) { 
      console.log(error); 
     } 
    }); 
} 

其中作爲我的PHP函數是

public function redeem_all() { 
    $user_id = $this->session->userdata('user_id'); 
    if (!empty($user_id)) { 
     $co_id = $this->input->post('co_id'); 
     $this->db->set('co_ytd_points','0', false); 
     $this->db->where('co_id' , $co_id); 
    } else { 
     $this->load->view('pages/login'); 
    } 
} 

當我調試上谷歌瀏覽器的代碼是確切的錯誤:

RESP =對象{readyState的:4,responseText的: 「」 ,狀態:200, statusText:「OK」},error =「parsererror」

+1

ParseError *什麼,在哪裏?* –

+0

包括你請了整個錯誤。 – Sander

+0

所以收到的數據不是一個有效的json ...你有沒有通知或smth? –

回答

0

我認爲這部分是決策問題:

data:'co_id='+co_id, 

它應該是這樣的:

data: {'co_id': co_id} 
+0

仍然錯誤仍然存在。 –

+1

您是否需要以JSON格式或標準POST數據格式發送數據? –

+0

我們需要通過JSON獲取數據 –

0

簡單地發佈使用JSON對象數據,如

$.ajax({ 
     type:'POST', 
     url:path, 
     data:{"co_id" : co_id}, 
     dataType:'json', 
     success:function(resp) 
     { 
      console.log("success!!="+resp); 
     }, 
     error:function(resp, error){ 
      console.log(error); 
     } 
    }); 
1

仍然有錯誤,因爲從價值迴歸PHP文件不是JSON格式
PHP打開標記後添加此項

header("Content-Type: application/json"); 

使JSON格式確保數據回用

echo json_encode($yourVarible);