第4行的解析错误:
... required.<\/p>\n"}{ "error": "<p>T
----------------------^
Expecting 'EOF', '}', ',', ']'
以下json回复:
{
"st": 0,
"msg": "<p>The Title field is required.<\/p>\n<p>The Description field is required.<\/p>\n<p>The URL field is required.<\/p>\n<p>The Posted On field is required.<\/p>\n<p>The Expire Date field is required.<\/p>\n"
}{
"error": "<p>The upload path does not appear to be valid.<\/p>"
}
这是我的控制器文件:
public function addnews()
{
$userfile = 'userfile';
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[5]');
$this->form_validation->set_rules('description', 'Description', 'trim|required');
$this->form_validation->set_rules('url', 'URL', 'trim|required');
$this->form_validation->set_rules('userfile', 'Image', 'trim');
$this->form_validation->set_rules('postedon', 'Posted On', 'trim|required');
$this->form_validation->set_rules('source', 'Source','trim|required');
$this->form_validation->set_rules('expiredate', 'Expire Date', 'trim|required');
if($this->form_validation->run() == FALSE)
{
echo json_encode(array('st'=>0, 'msg' => validation_errors()));
}
/******* extracting file extension ********/
$org_filename = $_FILES['userfile']['name'];
$path_parts = pathinfo($org_filename);
//$file_extension = $path_parts['extension'];
/***** end extracting file extension ******/
$config['upload_path'] = './vadmin/uploads/news';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024 * 1000;
//$config['file_name'] = 'news_'.time().'.'.$file_extension;
//echo "<pre>";
//print_r($_FILES);
$this->load->library('upload', $config);
if (!$this->upload->do_upload($userfile))
{
//echo '<br>entered into upload failed block.. ';
$error = array('error' => $this->upload->display_errors());
echo json_encode($error);
}
else
{
/**** assignment of post data ******/
$title = $this->input->post('title');
$description = $this->input->post('description');
$url = $this->input->post('url');
$userfile = $this->input->post('userfile');
$postedon = $this->input->post('postedon');
$source = $this->input->post('source');
$expiredate = $this->input->post('expiredate');
$status = $this->default_news_status ;
/**** end assignment of post data ******/
/**** load news model to insert reocrd into table *****/
$this->load->model('vadmin/Newsmodel','',true);
$this->Newsmodel->addRequestForm();
/**** load news model to insert reocrd into table *****/
//print jsone response for ajax request
echo json_encode(array('st'=>1, 'msg' => 'Successfully Submitted'));
}
}
这是我的观点脚本:
function ajaxFileUpload()
{
$.ajaxFileUpload
(
{
url:'<?php echo base_url();?>vadmin/ajax/addnews/',
secureuri:false,
fileElementId:'userfile',
dataType: 'JSON',
data: {title:$('#title').val(),description:$('#description').val(),url:$('#url').val(),userfile:$('#userfile').val(),postedon:$('#datepicker').val(),source:$('#source').val(),expiredate:$('#datepicker1').val()},
/*beforeSend:function()
{
$("#loading").show();
},
complete:function()
{
$("#loading").hide();
}, */
success: function (data)
{
if(data.st == 0)
{
$('#message').css('color','red');
$('#message').html(data.error).show(400);
}
if(data.st == 1)
{
$('#message').css('color','green');
$('#message').html(data.msg).show(400);
}
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.msg);
}
}
},
/*error: function (resp, status, e)
{
alert('ajax error :: '+e);
} */
}
);
return false;
}
答案 0 :(得分:0)
}
和{
之间应该有一个逗号。这种情况正在发生,因为您正在做两个单独的json_encode
和echo
两个。
尝试这样的事情:
$response = array();
if($this->form_validation->run() === false) // note: triple ===
{
$response['st'] = 0;
$response['msg'] = validation_errors();
}
及以下:
if (!$this->upload->do_upload($userfile)) {
$response['error'] = $this->upload->display_errors();
}
else {
// ..
$response['st'] = 1;
$response['msg'] = 'Successfully Submitted';
}
然后在底部做一个echo json_encode($response);
。