Sto eseguendo il debug di questo ajax per un bel po 'di tempo. Ho questo sul mio file jQuery:json_encode non funziona con una stringa html come valore
$("#typeForm").ajaxForm({
success : function(html){
alert(html);
}).submit();
Questo chiama service.php, e al suo interno ho questo:
$data = array('upload_data' => $this->upload->data());
$str = "<div style='position:relative'><img src='/assets/ui/success.png' /><span style='position:relative;top:-15px;'>Nachricht empfangen!</span></div>";
echo json_encode(array('file_name' => $data['upload_data']['file_name'], 'prompt' => $str));
Questo non funziona. Sostituendo lo $str
con lo $str = "HELLO WORLD";
, jQuery avvisa cosa devo aspettarmi. Quello che sembra essere il problema?
EDIT:
Ecco uno screenie dell'uscita:
Lo fa avvisi, ma se modifico il mio jQuery in questo:
$("#typeForm").ajaxForm({
success : function(html){
var obj = $.parseJSON(html);
alert(obj);
}).submit();
Poi lo fa Niente affatto, persino allarmante.
ho fatto un var_dump sul json_encode
e qui è la discarica, sembra un JSON malformata:
string(214) "{"file_name":"cde595988d386529909ce5a8fe3a6d6f.png","prompt":"<div style="position:relative;"><img src="\/assets\/ui\/success.png" \=""><span style="position:relative;top:-15px;">Nachricht empfangen!<\/span><\/div>"}"
</span></div>
Ecco il contenuto pieno di service.php
class Service extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$filename = 'uploadfile';
$config['upload_path'] = './uploads/temp';
$config['allowed_types'] = 'jpg|png|gif|doc|docx|pdf|ppt|pptx|xls|xlsx|bmp';
$config['max_size'] = '3072';
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload($filename))
{
$error = array('error' => $this->upload->display_errors());
echo json_encode(array('error' => $error['error']));
}
else
{
$data = array('upload_data' => $this->upload->data());
$file_name = $data['upload_data']['file_name'];
//print_r($data);
//echo json_encode(array('test' => "Hello World"));
$str = "<div style='position:relative;'><img src='/assets/ui/success.png' /><span style='position:relative;top:-15px;'>Nachricht empfangen!</span></div>";
$str2 = json_encode(array("file_name" => $file_name, "prompt" => $str));
//var_dump($str2);
exit(json_encode(array('file_name' => $data['upload_data']['file_name'], 'prompt' => $str)));
}
}
}
tuo JSON sembra .. .? –
di "non funziona" vuoi dire che sta generando errori? O non inserisce l'HTML o ..? –
Come funziona "non funziona"? A JSON non può importare di meno COSA si sta incorporando nella stringa: può essere numeri, può essere testo, può essere html. non importa, a patto che le regole di sintassi JSON siano rispettate. –