Answer the question
In order to leave comments, you need to log in
How to correctly transfer an array from php json to ajax?
Broke my head already, I'm new to this.
How to correctly transfer an array from php json to ajax?
Here is a piece of php code
$data['options'] = array();
foreach ($this->model_catalog_product->getProductOptions($_POST['calc_id']) as $option) {
$product_option_value_data = array();
foreach ($option['product_option_value'] as $option_value) {
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
$price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false));
} else {
$price = false;
}
$json['calc_ids']['product_option_value_data']['product_option_value_id'][] = $option_value['product_option_value_id'];
$json['calc_ids']['product_option_value_data']['option_value_id'][] = $option_value['option_value_id'];
$json['calc_ids']['product_option_value_data']['name'][] = $option_value['name'];
$json['calc_ids']['product_option_value_data']['price'][] = $price;
$json['calc_ids']['product_option_value_data']['price_prefix'][] = $option_value['price_prefix'];
}
}
$json['calc_ids']['options']['product_option_id'][] = $option['product_option_id'];
$json['calc_ids']['options']['product_option_value'][] = $product_option_value_data;
$json['calc_ids']['options']['option_id'][] = $option['option_id'];
******* $json['calc_ids']['options']['name'][] = $option['name']; *******
$json['calc_ids']['options']['type'][] = $option['type'];
$json['calc_ids']['options']['value'][] = $option['value'];
$json['calc_ids']['options']['required'][] = $option['required'];
}
}
$this->response->setOutput(json_encode($json));
$(document).ready(function () {
$('#calc_id').change(function () {
var calc_id = $(this).val();
$('#option_id').attr('disabled', true);
$('#option_id').html('<option>загрузка...</option>');
$.ajax({
url: 'index.php?route=module/featured/write',
type: 'post',
dataType: 'json',
data: 'calc_id='+calc_id,
success: function (data) {
var obj = eval(data);
var options = '';
$(obj.calc_ids.options.name).each(function() {
******* alert(obj.calc_ids.options.name); *********
options += '<option value="">' + obj.calc_ids.options.name + '</option>';
});
$('#option_id').html(options);
$('#option_id').attr('disabled', false);
}
});
});
});
Answer the question
In order to leave comments, you need to log in
Figured it out at last
var obj = eval(data);
var options = '';
$(obj.calc_ids.options.name).each(function(index) {
alert(data['calc_ids']['options']['name'][index]);
options += '<option value="">' + data['calc_ids']['options']['name'][index] + '</option>';
});
It needs to be an object, not an array. That is, in json, the first character should be { and not [. Put all the data in some array, for example response, and then encode it in json. I also racked my brain for a long time.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question