Php AJAX返回语法错误

Php AJAX返回语法错误,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我在发送数据时遇到了ajax问题。Ajax返回语法错误,我不知道为什么。任何帮助都将不胜感激 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:

我在发送数据时遇到了ajax问题。Ajax返回语法错误,我不知道为什么。任何帮助都将不胜感激

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函数在哪里

公共功能赎回所有(){
$user\u id=$this->session->userdata('user\u id');
如果(!empty($user_id)){
$co_id=$this->input->post('co_id');
$this->db->set('co_ytd_points','0',false);
$this->db->where('co\u id',$co\u id);
}否则{
$this->load->view('page/login');
}  
}
我在google chrome上调试代码时的确切错误是:

resp=对象{readyState:4,responseText:,状态:200, statusText:“OK”},error=“parsererror”


我认为这一部分正在制造问题:

data:'co_id='+co_id,
应该是这样的:

data: {'co_id': co_id}

只需使用类似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);
        }   
    }); 

仍然有错误,因为PHP文件返回的值不是JSON格式的
在PHP打开标记后添加此标记

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

echo json_encode($yourVarible);

ParseError什么,在哪里?请包括您得到的整个错误。因此,收到的数据不是有效的json…您有通知或smth吗?请附上日志您在ajax中使用的
数据类型:“json”
,因此如果输出不是json,它将给出ParseError。如果您将其替换为
dataType:'html'
,它将给出结果,然后您可以找出错误的地方您需要以JSON格式或标准POST数据格式发送数据吗?我们需要通过JSONI获取数据是否可能co_id变量未定义或为空?我已经尝试添加硬编码值作为数据:{“co_id”:1}但仍然存在错误。我感觉这是响应解析错误,而不是请求