1

I have a module that I need to do a php GET action and get some informations from php file. The problem is that the returned value is the Dashboard html.

Faturamento/Controller/Adminhtml/Ac/Index.php

<?php

namespace PereiraTech\Faturamento\Controller\Adminhtml\Ac;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Backend\App\Action
{

    protected $resultPageFactory;
    public $_storeManager;


    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,

        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory

    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;    
        $this->resultJsonFactory = $resultJsonFactory;

    }

    /**
     * Load the page defined in view/adminhtml/layout/exampleadminnewpage_helloworld_index.xml
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {


        $params = $this->getRequest()->getParams();

        //Send response to Ajax query
        $data = 200;
        $result = $this->resultJsonFactory->create();
        return $result->setData($data);

    }
}

Faturamento/view/adminhtml/web/js/form/element/options.js

## Heading ##define([      
    'jquery',
    'uiRegistry',  
    'Magento_Ui/js/form/components/button',
    'Magento_Ui/js/modal/modal'
], function ($,uiRegistry, select, modal) {
    'use strict';

    return select.extend({


    action: function(){            
            var ajaxRequest;
            ajaxRequest = jQuery.ajax({
                url: "faturamento_fat/Ac/Index",
                type: 'GET',
                data: {id : 1, form_key: window.FORM_KEY}, //exemple
                beforeSend: function () {

                }
            });
                //Show successfully for submit message
            ajaxRequest.done(function (response, textStatus, jqXHR) {
                console.log(textStatus);
                console.log(response);
            });

                //On failure of request this function will be called
            ajaxRequest.fail(function (xhr, status, error) {
                console.log(status);
                console.log(error);
                console.log(xhr.responseText);
                //show error
                console.log('rou');
            });
            return this._super();
        },
    });
});

routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="faturamento_fat" frontName="faturamento_fat">
            <module name="PereiraTech_Faturamento"/>
        </route>
    </router>
</config>
  • use url: "/faturamento_fat/Ac/Index", in ajax URL. Adding slash (/) before URL will add URI after baseUrl. – Shoaib Munir Apr 13 at 4:14
  • Tried, but received 404 error – Murilo Pereira Apr 13 at 11:53
  • browser URLs are case sensitive, may be you need to use all with small letter "/faturamento_fat/ac/index" – Shoaib Munir Apr 13 at 19:29
  • Tried too, but now I receive the dashboard's html, I've tried to post to one page that I know that worked, and received it html – Murilo Pereira Apr 13 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.