1

I'm using custom js in Product Add/Edit page of magento 2 admin. I need to make a ajax call when the product save button clicked. I used the below code.

require(['jquery', 'domReady!'], function ($) {
    $(document).ajaxComplete(function () {
        let data = '';
        varr.forEach((i, j) => {
            if (i.length > 0) {
               data = JSON.stringify({
                  "data": {
                     "sku": sku,
                     "brand": i[0].brand,
                     "variant": i[0].variant,
                     "vehicle_type": "car",
                     "model": i[0].model
                  }
               });
            }

            var settings = {
               "async": true,
               "crossDomain": true,
               "url": "http://192.168.2.196/motogaadi/rest/V1/garage/garage_sku_insert/",
               "method": "POST",
               "headers": {
                 "Content-Type": "application/json",
                 "Cache-Control": "no-cache",
                 "cache-control": "no-cache"
               },
               "processData": false,
               "data": data
             }

             $.ajax(settings).done(function (response) {
               console.log(response);
             });

      });
});

});

I am receiving the below issue.

POST http://192.168.2.196/motogaadi/rest/V1/garage/garage_sku_insert/?isAjax=true 400 (Bad Request)

The same Ajax call is working in frontend but not in adminhtml.

1
  • thanks but it's not working.
    – Ramesh KR
    Jan 7, 2020 at 8:01

1 Answer 1

0

In your ajax setting modified like this:

    var settings = {
       "async": true,
       "crossDomain": true,
       "url": "http://192.168.2.196/motogaadi/rest/V1/garage/garage_sku_insert/",
       "method": "POST",
       "headers": {
         "Content-Type": "application/json",
         "Cache-Control": "no-cache",
         "cache-control": "no-cache"
       },
       "processData": false,
       "data": data,
       beforeSend: function (xhr) {
        //Empty to remove magento's default handler
    },

}

1
  • 1
    beforeSend option must be empty. This is important. Jan 5, 2021 at 4:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.