2

Im try to pass sql query value as an AJAX request, It have a array object I need to pass only the value, below code for that

public function execute()
    {
$resultJson = $this->resultJsonFactory->create();

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); 
        $connection = $resource->getConnection(); 

        $sql = "SELECT price_component FROM base_price WHERE width =".$this->getRequest()->Width." AND height =".$this->getRequest()->Height; 
        $result = $connection->fetchAll($sql);
        return $resultJson->setData($result);
    } 

if I used below code in .phtml class

.done(function(data) {
                    console.log(data);
                });

result will be like below

[{…}]
0
:
{price_component: "122"}
length
:
1
__proto__
:
Array(0)

If i use result[0] index it will be like JSON object like below

{price_component: "132"}
price_component
:
"132"
__proto__
:
Object

I just need to get value to the console how can I do that

1 Answer 1

3

You can try in your ajax success response like below,

.done(function(data) {
          var priceValue = data.price_component;
          console.log(data.price_component);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.