How to make ajax call in magento admin form












2














In my magento admin form, I have added a category field. Based on the selection of category in category field an ajax call should be done.



$category = $fieldSet->addField(
'category_id', 'select', [
'name' => 'category_id',
'label' => __('Category'),
'required' => true,
'values' => $this->_selectedCategories->toOptionArray(),
]
);


I have set ajax call for that field



  $url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl');

$category->setAfterElementHtml('
<script>
require(['jquery'], function($){
var customUrl =
$("#category_id").on('change', function () {
$.ajax({
url: ''.$url.'',
type: 'POST',
data: {category_id: $(this).val(), form_key: window.FORM_KEY},
success: function (response) {
$("#url").val(response['content']);
}
});
});
});
</script>
');


Now how to set url for ajax call?



url: '<?= $url; ?>'


I am not getting controller call using this line. How to set $url as a ajax call url in script?










share|improve this question





























    2














    In my magento admin form, I have added a category field. Based on the selection of category in category field an ajax call should be done.



    $category = $fieldSet->addField(
    'category_id', 'select', [
    'name' => 'category_id',
    'label' => __('Category'),
    'required' => true,
    'values' => $this->_selectedCategories->toOptionArray(),
    ]
    );


    I have set ajax call for that field



      $url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl');

    $category->setAfterElementHtml('
    <script>
    require(['jquery'], function($){
    var customUrl =
    $("#category_id").on('change', function () {
    $.ajax({
    url: ''.$url.'',
    type: 'POST',
    data: {category_id: $(this).val(), form_key: window.FORM_KEY},
    success: function (response) {
    $("#url").val(response['content']);
    }
    });
    });
    });
    </script>
    ');


    Now how to set url for ajax call?



    url: '<?= $url; ?>'


    I am not getting controller call using this line. How to set $url as a ajax call url in script?










    share|improve this question



























      2












      2








      2







      In my magento admin form, I have added a category field. Based on the selection of category in category field an ajax call should be done.



      $category = $fieldSet->addField(
      'category_id', 'select', [
      'name' => 'category_id',
      'label' => __('Category'),
      'required' => true,
      'values' => $this->_selectedCategories->toOptionArray(),
      ]
      );


      I have set ajax call for that field



        $url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl');

      $category->setAfterElementHtml('
      <script>
      require(['jquery'], function($){
      var customUrl =
      $("#category_id").on('change', function () {
      $.ajax({
      url: ''.$url.'',
      type: 'POST',
      data: {category_id: $(this).val(), form_key: window.FORM_KEY},
      success: function (response) {
      $("#url").val(response['content']);
      }
      });
      });
      });
      </script>
      ');


      Now how to set url for ajax call?



      url: '<?= $url; ?>'


      I am not getting controller call using this line. How to set $url as a ajax call url in script?










      share|improve this question















      In my magento admin form, I have added a category field. Based on the selection of category in category field an ajax call should be done.



      $category = $fieldSet->addField(
      'category_id', 'select', [
      'name' => 'category_id',
      'label' => __('Category'),
      'required' => true,
      'values' => $this->_selectedCategories->toOptionArray(),
      ]
      );


      I have set ajax call for that field



        $url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl');

      $category->setAfterElementHtml('
      <script>
      require(['jquery'], function($){
      var customUrl =
      $("#category_id").on('change', function () {
      $.ajax({
      url: ''.$url.'',
      type: 'POST',
      data: {category_id: $(this).val(), form_key: window.FORM_KEY},
      success: function (response) {
      $("#url").val(response['content']);
      }
      });
      });
      });
      </script>
      ');


      Now how to set url for ajax call?



      url: '<?= $url; ?>'


      I am not getting controller call using this line. How to set $url as a ajax call url in script?







      magento2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 5:10









      Milind Singh

      603115




      603115










      asked Nov 21 '18 at 4:46









      PavitraPavitra

      1148




      1148






















          3 Answers
          3






          active

          oldest

          votes


















          4















          1. Your URL should not have a session key. Remove as below.


          $url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl', ['_nosid' => true]);




          1. Correct the way to add a variable in the string.


          $category->setAfterElementHtml('
          <script>
          require(['jquery'], function($){
          var customUrl =
          $("#category_id").on('change', function () {

          $.ajax({
          url: ''.$url.'',
          type: 'POST',
          data: {category_id: $(this).val(), form_key: window.FORM_KEY},
          success: function (response) {
          $("#url").val(response['content']);
          }
          });
          });
          });
          </script>
          ');






          share|improve this answer

















          • 1




            Its Working !!!. Thank you.
            – Pavitra
            Nov 21 '18 at 5:04



















          2














          Try to use this below way :



          $category->setAfterElementHtml('
          <script>
          require(['jquery'], function($){
          var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';
          var customUrl =
          $("#category_id").on('change', function () {
          $.ajax({
          url: url,
          type: 'POST',
          data: {category_id: $(this).val(), form_key: window.FORM_KEY},
          success: function (response) {
          $("#url").val(response['content']);
          }
          });
          });
          });
          </script>
          ');





          share|improve this answer





























            0














            You can get URL using below code.



            var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';





            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "479"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f250730%2fhow-to-make-ajax-call-in-magento-admin-form%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4















              1. Your URL should not have a session key. Remove as below.


              $url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl', ['_nosid' => true]);




              1. Correct the way to add a variable in the string.


              $category->setAfterElementHtml('
              <script>
              require(['jquery'], function($){
              var customUrl =
              $("#category_id").on('change', function () {

              $.ajax({
              url: ''.$url.'',
              type: 'POST',
              data: {category_id: $(this).val(), form_key: window.FORM_KEY},
              success: function (response) {
              $("#url").val(response['content']);
              }
              });
              });
              });
              </script>
              ');






              share|improve this answer

















              • 1




                Its Working !!!. Thank you.
                – Pavitra
                Nov 21 '18 at 5:04
















              4















              1. Your URL should not have a session key. Remove as below.


              $url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl', ['_nosid' => true]);




              1. Correct the way to add a variable in the string.


              $category->setAfterElementHtml('
              <script>
              require(['jquery'], function($){
              var customUrl =
              $("#category_id").on('change', function () {

              $.ajax({
              url: ''.$url.'',
              type: 'POST',
              data: {category_id: $(this).val(), form_key: window.FORM_KEY},
              success: function (response) {
              $("#url").val(response['content']);
              }
              });
              });
              });
              </script>
              ');






              share|improve this answer

















              • 1




                Its Working !!!. Thank you.
                – Pavitra
                Nov 21 '18 at 5:04














              4












              4








              4







              1. Your URL should not have a session key. Remove as below.


              $url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl', ['_nosid' => true]);




              1. Correct the way to add a variable in the string.


              $category->setAfterElementHtml('
              <script>
              require(['jquery'], function($){
              var customUrl =
              $("#category_id").on('change', function () {

              $.ajax({
              url: ''.$url.'',
              type: 'POST',
              data: {category_id: $(this).val(), form_key: window.FORM_KEY},
              success: function (response) {
              $("#url").val(response['content']);
              }
              });
              });
              });
              </script>
              ');






              share|improve this answer













              1. Your URL should not have a session key. Remove as below.


              $url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl', ['_nosid' => true]);




              1. Correct the way to add a variable in the string.


              $category->setAfterElementHtml('
              <script>
              require(['jquery'], function($){
              var customUrl =
              $("#category_id").on('change', function () {

              $.ajax({
              url: ''.$url.'',
              type: 'POST',
              data: {category_id: $(this).val(), form_key: window.FORM_KEY},
              success: function (response) {
              $("#url").val(response['content']);
              }
              });
              });
              });
              </script>
              ');







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 21 '18 at 5:01









              Milind SinghMilind Singh

              603115




              603115








              • 1




                Its Working !!!. Thank you.
                – Pavitra
                Nov 21 '18 at 5:04














              • 1




                Its Working !!!. Thank you.
                – Pavitra
                Nov 21 '18 at 5:04








              1




              1




              Its Working !!!. Thank you.
              – Pavitra
              Nov 21 '18 at 5:04




              Its Working !!!. Thank you.
              – Pavitra
              Nov 21 '18 at 5:04













              2














              Try to use this below way :



              $category->setAfterElementHtml('
              <script>
              require(['jquery'], function($){
              var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';
              var customUrl =
              $("#category_id").on('change', function () {
              $.ajax({
              url: url,
              type: 'POST',
              data: {category_id: $(this).val(), form_key: window.FORM_KEY},
              success: function (response) {
              $("#url").val(response['content']);
              }
              });
              });
              });
              </script>
              ');





              share|improve this answer


























                2














                Try to use this below way :



                $category->setAfterElementHtml('
                <script>
                require(['jquery'], function($){
                var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';
                var customUrl =
                $("#category_id").on('change', function () {
                $.ajax({
                url: url,
                type: 'POST',
                data: {category_id: $(this).val(), form_key: window.FORM_KEY},
                success: function (response) {
                $("#url").val(response['content']);
                }
                });
                });
                });
                </script>
                ');





                share|improve this answer
























                  2












                  2








                  2






                  Try to use this below way :



                  $category->setAfterElementHtml('
                  <script>
                  require(['jquery'], function($){
                  var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';
                  var customUrl =
                  $("#category_id").on('change', function () {
                  $.ajax({
                  url: url,
                  type: 'POST',
                  data: {category_id: $(this).val(), form_key: window.FORM_KEY},
                  success: function (response) {
                  $("#url").val(response['content']);
                  }
                  });
                  });
                  });
                  </script>
                  ');





                  share|improve this answer












                  Try to use this below way :



                  $category->setAfterElementHtml('
                  <script>
                  require(['jquery'], function($){
                  var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';
                  var customUrl =
                  $("#category_id").on('change', function () {
                  $.ajax({
                  url: url,
                  type: 'POST',
                  data: {category_id: $(this).val(), form_key: window.FORM_KEY},
                  success: function (response) {
                  $("#url").val(response['content']);
                  }
                  });
                  });
                  });
                  </script>
                  ');






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 4:53









                  Emipro Technologies Pvt. Ltd.Emipro Technologies Pvt. Ltd.

                  2,4541823




                  2,4541823























                      0














                      You can get URL using below code.



                      var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';





                      share|improve this answer




























                        0














                        You can get URL using below code.



                        var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';





                        share|improve this answer


























                          0












                          0








                          0






                          You can get URL using below code.



                          var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';





                          share|improve this answer














                          You can get URL using below code.



                          var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 21 '18 at 6:47

























                          answered Nov 21 '18 at 6:35









                          Kishan PatadiaKishan Patadia

                          3,5301923




                          3,5301923






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Magento Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f250730%2fhow-to-make-ajax-call-in-magento-admin-form%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Costa Masnaga

                              Fotorealismo

                              Sidney Franklin