1

I'm using Magento for my development. I need to make ajax call to the same page to fetch some detail from related php function. As I checked in the developer tool, under network tab, the call is made to the current page but it displays the following error:

Invalid URL: adminpteb/efund/efund.phtml

efund.phtml is the page I'm where the ajax call is initiated and the php function to be invoked also resides within the same page. Is the Magento has own way of fetching php scripts via ajax?

Referred to this link: Using Basic AJAX calls within Magento

And I defined the URl like this: url: /adminpteb/efund/efund.phtml, but still same error.

the ajax alerts 'complete' and correct function is picked up. However the value returned by function 1 is not reaching the ajax. My script:

$.ajax({
      method: "GET",
      url: "efund.phtml",
      data: { function_to_call: 0, id: cl[3] }
    }).fail(function() {
      alert( "error" );
    }).always(function() {
       alert( "complete" );
    }).done(function( msg ) {
        var obj = jQuery.parseJSON( msg );
        alert( "ID obtained from server is " + obj.id );
      });

PHP script

switch ($_GET['function_to_call']) 
     {

         case 0:
         {   
               function1($_GET['id']);
               break;
         }

        case 1: 
        {
              function2();
              break;
        }
        default: break;

     }



        function function1() {
            echo "ID= ".$_GET['id'];
             return json_encode(array ( "id" => $param ) );
        }
        function function2() {
             echo "This is function 2";
    }
Community
  • 1
  • 1
112233
  • 2,406
  • 3
  • 38
  • 88

1 Answers1

1

You can get the current page URL in magento by the following code.

$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();

And the pass this url to your ajax call

$.ajax({
    method: "GET",
    url: '<?php echo path; ?>',
    data: {function_to_call: 0, id: cl[3]}
}).fail(function () {
    alert("error");
}).always(function () {
    alert("complete");
}).done(function (msg) {
    var obj = jQuery.parseJSON(msg);
    alert("ID obtained from server is " + obj.id);
});
urfusion
  • 5,528
  • 5
  • 50
  • 87
  • it make the call to 'efund' which is the folder name..However doesn't return value as expected from the function. – 112233 Mar 03 '16 at 07:20
  • it must alert this: alert("ID obtained from server is " + obj.id); – 112233 Mar 03 '16 at 07:33
  • it will not alert that. because every time the ajax call it will render whole page. so your `obj.id` will not execute. try `alert(msg)`. And I recommend you to call ajax by a controller. which is a standard approach . – urfusion Mar 03 '16 at 07:46
  • yes it renders the whole page. I assume it doesn't call the function 1? Then how the ajax returns 'complete'? And if I still need to make call to the php function and get back value from it, what is they way? – 112233 Mar 03 '16 at 08:55
  • the best way to do these kind of task is create a custom module and inherit the functionality that you want then call your ajax code to the controller. – urfusion Mar 03 '16 at 09:19
  • https://www.atwix.com/magento/ajax-requests-in-magento/. This tutorial helps you in better way. – urfusion Mar 03 '16 at 09:20