0

I am not able to find correct approaches to done this work. I found some resources but there they guys are using model files also but they didn't explain about that files. My codes are below

Controller file

namespace Vendor\Modulename\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\ResultFactory;



class Index extends Action
{
    /**
     * @var \Tutorial\SimpleNews\Model\NewsFactory
     */
    protected $resultJsonFactory;
    protected $resultPageFactory;

/**
 * @param Context $context
 * @param NewsFactory $modelNewsFactory
 */



public function __construct(
        Context                                             $context,
        \Magento\Framework\Controller\Result\JsonFactory    $resultJsonFactory,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory            = $resultJsonFactory;
        parent::__construct($context);
    }


   public function execute() {


    $result = $this->resultJsonFactory->create();
    if ($this->getRequest()->isAjax())) 
    {

        $test=Array
        (
            'Firstname' => 'What is your firstname',
            'Email' => 'What is your emailId',
            'Lastname' => 'What is your lastname',
            'Country' => 'Your Country'
        );
        return $result->setData($test);
    }
}
}

Template file

data = jQuery(this).serialize();
            jQuery.ajax({
               showLoader: true,
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "modulename/index/index", 
                data: "label=" + abc,
                success: function (result) { jQuery('#result').html(result); },
                error: function (error) { alert(error); }

Please somebody guide me

4
  • Your Ajax cannot work? Commented Jan 11, 2017 at 6:26
  • Then how it will work. If you know then please guide me Commented Jan 11, 2017 at 6:30
  • what to you receive in response in your javascript ? Commented Jan 11, 2017 at 6:49
  • Error block has been working Commented Jan 11, 2017 at 6:51

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.