- 24 Jun 2019, 17:48
#82523
Boa tarde,
Estou tentando levar um valor de uma option da minha view até o model e retornar com os valores.
Consegui enviar o valor da view para o controller, mas o json de retorno está dando o seguinte erro:
VM3050:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Function.n.parseJSON (jquery-2.1.1.min.js?j2v=2.11:4)
at Function.e.parseJSON (jquery-migrate-1.2.1.min.js?j2v=2.11:2)
at Object.success (index.php?route=account/customerpartner/addproduct:1705)
at j (jquery-2.1.1.min.js?j2v=2.11:2)
at Object.fireWith [as resolveWith] (jquery-2.1.1.min.js?j2v=2.11:2)
at x (jquery-2.1.1.min.js?j2v=2.11:4)
at XMLHttpRequest.<anonymous> (jquery-2.1.1.min.js?j2v=2.11:4)
n.parseJSON @ jquery-2.1.1.min.js?j2v=2.11:4
e.parseJSON @ jquery-migrate-1.2.1.min.js?j2v=2.11:2
success @ index.php?route=account/customerpartner/addproduct:1705
j @ jquery-2.1.1.min.js?j2v=2.11:2
fireWith @ jquery-2.1.1.min.js?j2v=2.11:2
x @ jquery-2.1.1.min.js?j2v=2.11:4
(anonymous) @ jquery-2.1.1.min.js?j2v=2.11:4
load (async)
send @ jquery-2.1.1.min.js?j2v=2.11:4
ajax @ jquery-2.1.1.min.js?j2v=2.11:4
(anonymous) @ index.php?route=account/customerpartner/addproduct:1697
dispatch @ jquery-2.1.1.min.js?j2v=2.11:3
r.handle @ jquery-2.1.1.min.js?j2v=2.11:3
Seguem os códigos que fiz.
Controller
Se puder me ajudar a resolver o erro, agradeço!
Estou tentando levar um valor de uma option da minha view até o model e retornar com os valores.
Consegui enviar o valor da view para o controller, mas o json de retorno está dando o seguinte erro:
VM3050:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Function.n.parseJSON (jquery-2.1.1.min.js?j2v=2.11:4)
at Function.e.parseJSON (jquery-migrate-1.2.1.min.js?j2v=2.11:2)
at Object.success (index.php?route=account/customerpartner/addproduct:1705)
at j (jquery-2.1.1.min.js?j2v=2.11:2)
at Object.fireWith [as resolveWith] (jquery-2.1.1.min.js?j2v=2.11:2)
at x (jquery-2.1.1.min.js?j2v=2.11:4)
at XMLHttpRequest.<anonymous> (jquery-2.1.1.min.js?j2v=2.11:4)
n.parseJSON @ jquery-2.1.1.min.js?j2v=2.11:4
e.parseJSON @ jquery-migrate-1.2.1.min.js?j2v=2.11:2
success @ index.php?route=account/customerpartner/addproduct:1705
j @ jquery-2.1.1.min.js?j2v=2.11:2
fireWith @ jquery-2.1.1.min.js?j2v=2.11:2
x @ jquery-2.1.1.min.js?j2v=2.11:4
(anonymous) @ jquery-2.1.1.min.js?j2v=2.11:4
load (async)
send @ jquery-2.1.1.min.js?j2v=2.11:4
ajax @ jquery-2.1.1.min.js?j2v=2.11:4
(anonymous) @ index.php?route=account/customerpartner/addproduct:1697
dispatch @ jquery-2.1.1.min.js?j2v=2.11:3
r.handle @ jquery-2.1.1.min.js?j2v=2.11:3
Seguem os códigos que fiz.
Controller
Código: Selecionar todos
Viewpublic function getProductCategID() {
$id = $this->request->post['id_product_category'];
$data['product_subcategories1'] = array();
$subcategory1_info = $this->model_account_customerpartner->getSubcategory1($id);
foreach ($subcategory1_info as $subcategory1) {
if ($subcategory1) {
$data['product_subcategories1'][] = array(
'level' => $subcategory1['level'],
'name' => $subcategory1['name'],
'parent_id' => $subcategory1['parent_id'],
'category_id' => $subcategory1['category_id']
);
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($data['product_subcategories1']));
}
}
}
Código: Selecionar todos
Obs: a função do controller que criei está fora da função "index". Não sei se isso é problema, mas vale a pena avisar<script>
$('#product-category').on('change',function(){
var value = $(this).val();
alert(value);
$.ajax({
url: "index.php?route=account/customerpartner/addproduct/getProductCategID/",
type: "post",
data: { 'id_product_category':value },
datatype: "json",
cache: false,
success: function (data) {
alert('success');
var price = jQuery.parseJSON(data);
console.log(price);
},
error: function(){
alert('failure');
}
});
});
</script>
Se puder me ajudar a resolver o erro, agradeço!