简体   繁体   English

JSONP SyntaxError:缺少; 声明前

[英]JSONP SyntaxError: missing ; before statement

I have on a controller a function on codeigniter which gets and checks image for my templates. 我在控制器上的codeigniter上有一个功能,该功能获取并检查模板的图像。

I have place a id="template" just after my thumbnail bootstrap div id template works with my script code. 我的缩略图引导程序div id模板与脚本代码一起使用后,我放置了id =“ template”。

Currently when I select theme I get a firebug error 目前,当我选择主题时,出现萤火虫错误

Error Pop Up 错误弹出

SyntaxError: missing ; before statement
OK
{"image":"<img src=\\\"http:\/\/localhost\/codeigniter\/codeigniter-cms\/image\/templates\/default.png\\\" alt=\\\"\\\"\/>"}

Controller Image Function 控制器影像功能

public function template() {
  $this->load->helper('html');
  if (file_exists(FCPATH . 'image/templates/' . $this->configs->get('config_template') . '.png') == FALSE) {
    $img =  addslashes(img('image/no_image.png'));
    echo json_encode(array('image'=>$img));
  } else {
  if($this->input->post('config_template') !== FALSE) {
    $img = addslashes(img('image/templates/' . basename($this->input->post('config_template')) . '.png'));
    echo json_encode(array('image'=>$img));
   }
  }
}

View 视图

<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-setting" class="form-horizontal">

<div class="form-group">
<label class="col-sm-2 control-label" for="input-template"><?php echo $entry_template; ?></label>
<div class="col-sm-10">
<select name="config_template" id="input-template" class="form-control">
<?php foreach ($templates as $template) { ?>
<?php if ($template == $config_template) { ?>
<option value="<?php echo $template; ?>" selected="selected"><?php echo $template; ?></option>
<?php } else { ?>
<option value="<?php echo $template; ?>"><?php echo $template; ?></option>
<?php } ?>
<?php } ?>
</select>


<div class="form-group">
<div class="col-lg-4">
<div class="img-thumbnail">
<div id="template"></div>
</div>
</div>
</div>

</form>

<script>
console.log('Jquery is working');
$('select[name="config_template"]').on('change', function () {
var template_name;
template_name = encodeURIComponent(this.value);
$.ajax({
type: 'POST',
dataType: 'jsonp',
url: '<?=base_url('admin/setting/template');?>'  + '/',
data: { config_template: template_name
},
complete: function () {
$('.fa-spin').remove();
},
success: function (data) {
$('.fa-spin').remove();
$('#template').html(data.image);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
})
</script>

Found out issue it was addslashes 找出问题是斜杠

old

public function template() {
  $this->load->helper('html');
  if (file_exists(FCPATH . 'image/templates/' . $this->configs->get('config_template') . '.png') == FALSE) {
    $img =  addslashes(img('image/no_image.png'));
    echo json_encode(array('image'=>$img));
  } else {
  if($this->input->post('config_template') !== FALSE) {
    $img = addslashes(img('image/templates/' . basename($this->input->post('config_template')) . '.png'));
    echo json_encode(array('image'=>$img));
   }
  }
}

new

public function template() {
$this->load->helper('html');
if (file_exists(FCPATH . 'image/templates/' . $this->configs->get('config_template') . '.png') == FALSE) {
$img = img('image/no_image.png');
echo json_encode(array('image'=>$img));
}else
{
if($this->input->post('config_template') !== FALSE)
{
$img = img('image/templates/' . $this->input->post('config_template') . '.png');
echo json_encode(array('image'=>$img));
}
}
}
  1. You are outputting JSON. 您正在输出JSON。 Not JSONP. 不是JSONP。 Change dataType: 'jsonp', to dataType: 'json', dataType: 'jsonp',更改为dataType: 'json',
  2. Adding slashes to some HTML will break that HTML. 在某些HTML中添加斜杠将破坏该HTML。 You'll be putting \\ characters in your image URLs and making them wrong. 您将在图片网址中添加\\字符,并使其错误。 Remove all uses of the addslashes function. 删除所有对addslashes函数的使用。 encode_json will do all the escaping you need. encode_json将完成您需要的所有转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM