2013-01-11 82 views
0

问题是我得到false的对话框,但查询运行良好,值已成功添加到数据库中。它应该打印真实,但它给了我一个错误。我通过Firebug检查了值res = 1正在进行,但我不知道什么是错误的。从控制器JSON传递一个值来查看CodeIgniter中的Ajax

笔者认为:

$.ajax({ 
    url: "<?php echo site_url('itemsController/additems'); ?>", 
    type: 'POST', 
    data: form_data, 
    success: function(msg) { 
     if(msg.res == 1) 
     { 
      alert(true); 
     } 
     else 
     { 
      alert(false); 
     } 
    } 
}); 

控制器:

 $result = array(); 
     $this->load->model('itemsModel'); 
     $query = $this->itemsModel->addItemstoDB($data); 

     if ($query){ //&& any other condition 
      $result['res'] = 1; 
     } 
     else 
     { 
      $result['res'] = 0; 
     } 
     echo json_encode($result); //At the end of the function. 
    } 
} 

回答

3

尝试设置你的dataTypejson所以从服务器发回被解析为JSON数据。

$.ajax({ 
    url: "<?php echo site_url('itemsController/additems'); ?>", 
    type: 'POST', 
    data: form_data, 
    dataType: 'json', 
    success: function(msg) { 
     if(msg.res == 1) { 
      alert(true); 
     } 
     else 
     { 
      alert(false); 
     } 
    } 
}); 
+0

是啊thanksssssssssssssssssssss ...我想定义一个json数据类型。 – mynameisjohn

-1

通知回报。

if ($query){ 
     $result['res'] = 1; 
    } 
    else 
    { 
     $result['res'] = 0; 
    } 
    return json_encode($result);//at the end of the function. 
} 
+1

你想回声,而不是返回。 –

+0

你必须回声不要返回。这里是ajax。 –

相关问题