2016-08-16 93 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);