1

In my .phtml i have:

require([
'jquery'
], function ($) {
 $.ajax({
        url: "get_shipment_info.php",
        type: "get", //send it through get method
        data: { 
            company: ManiFacturerName
        },
        success: function(response) {
},
        error: function(xhr) {
            console.log("ERR");
            //Delete the object
            var element = document.getElementById('shipment_info');
            element.parentNode.removeChild(element);
        }
  });
});

The php file

url: "get_shipment_info.php",

is just echo "1"; The problem is i get the response after 7seconds. I dont know why...

3
  • It is an issue with your server response. Don't think it is an issue with magento
    – Amit Bera
    Commented Jun 14, 2018 at 7:44
  • How so the page loads fine its just the ajax call being slow?
    – Compu Scie
    Commented Jun 14, 2018 at 8:03
  • This looks more like an infrastructure issue rather than magento, therefore more details about the setup should be included. for example, if you run the the cli that get_shipment_info.php - how long does it take to run.. that echo "1" is the only statement in the file or magento's bootstrap is included? etc.. Commented Dec 7, 2021 at 16:39

1 Answer 1

-1

You can just set below code in your phtml file to use ajax, You have to change your customurl in below code,

<script type="text/javascript">
    require(["jquery"],function($) {
        $(document).ready(function() {
            var customurl = "<?php echo $this->getUrl().'frontname/index/index'?>";
            $.ajax({
                url: customurl,
                type: 'POST',
                dataType: 'json',
                data: {
                    customdata1: 'test1',
                    customdata2: 'test2',
                },
            complete: function(response) {             
                country = response.responseJSON.default_country;
                state = response.responseJSON.state;         
                console.log(state+' '+country);   
                },
                error: function (xhr, status, errorThrown) {
                    console.log('Error happens. Try again.');
                }
            });
        });
    });
</script>

inside your controller file execute() method,

<?php
 use Magento\Framework\Controller\ResultFactory;
 public function execute()
    {
        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);

        $response = $this->resultFactory->create(ResultFactory::TYPE_RAW);
        $response->setHeader('Content-type', 'text/plain');
        $country = 'india';
        $state = 'gujarat';
        $response->setContents(
            $this->_jsonHelper->jsonEncode(
                [
                    'default_country' => $country,
                    'state' => $state,
                ]
            )
        );
        return $response;
    } 
4
  • could you be more specific this is for another example..
    – Compu Scie
    Commented Jun 13, 2018 at 11:43
  • this is not working
    – Compu Scie
    Commented Jun 13, 2018 at 12:20
  • any error show ?
    – user68116
    Commented Jun 13, 2018 at 12:25
  • i cant get it to working could you please be more spesific like, how do i link my custom url to execute method, also where my controller file is....
    – Compu Scie
    Commented Jun 13, 2018 at 12:29

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.