下面的腳本工作,據我可以告訴:jQuery的JSON不是將數據傳遞給正確的詞
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#add').bind('keypress', function(e) {
if(e.keyCode == 13){
var add = $("#add").val();
$.ajax({
type: "POST",
dataType: "JSON",
url: "<?php echo site_url("home/jsonAddData"); ?>",
data: add,
json: {title_posted: true},
success: function(data){
if(data.title_posted == true) { // true means data was successfully posted.
$("#success").append("Success").fadeIn(400);
} else if(data.title_posted == false) { // false means data failed to post.
$("#success").append('Failure').fadeIn(400);
}
}
});
}
});
});
</script>
我用下面的代碼遇到的問題是,MySQL的insetion查詢只是不會工作。它在表格中創建行並自動遞增,但由於某些奇怪的原因,它不會將以上Javascript中的「var add」傳遞給下面的Ci腳本,並在db中執行插入操作。任何想法或想法?
<?php
class home extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data = array();
$data['lists'] = $this->displayList();
$this->load->view('home', $data);
}
function displayList() {
$str = '';
$query = $this->db->query("SELECT * FROM data");
foreach ($query->result() as $row) {
$b = '<input name="completed" type="checkbox" />';
$a = $row->title . "<br>";
$str .= $b.$a;
}
return $str;
}
function jsonAddData() {
if($this->input->is_ajax_request()) {
$title = $this->input->post('title');
$query = $this->db->query("INSERT INTO data (title) VALUES ('$title')");
header('Content-type:application/json');
if($query) echo json_encode(array('title_posted' => true));
else echo json_encode(array('title_posted' => false));
}
}
}
?>
你有HTML注入(跨站腳本)和SQL注入(數據庫妥協),在該腳本安全漏洞。對輸出到頁面'htmlspecialchars($ row-> title)'的所有數據進行HTML編碼並使用參數化查詢來避免這種情況。 – bobince 2011-12-27 18:19:32