1

Using Magento 2, Ajax in phtml, Admin. I got an error while trying to request API by ajax from template of sale order page.

AJAX

<script type="text/javascript">
require(['jquery', 'domReady!'], function($){
    changeDriver = function () {
        var driverBase = 'https://127.0.0.1:445/index.php/rest/V1/driver'
        var data = {"orderId": "58", "driverId": "20"};
        $.ajax({
            showLoader: true,
            url: driverBase,
            type: "POST",
            data: data,
            dataType: 'json',
            contentType: "application/json; charset=utf-8"

        }).done(function (data) {
            console.log(data);
            $('#driver_detail').html('Driver chnaged.');

        }).fail(function (jqXhr) {
            console.log(jqXhr);
            $('#driver_detail').html('Cannot change driver.');
        });
    }
});
</script>

Header

**General**
Request URL: https://127.0.0.1:445/index.php/rest/V1/driver/?isAjax=true
Request Method: POST
Status Code: 400 Bad Request
Remote Address: 127.0.0.1:445
Referrer Policy: no-referrer-when-downgrade

**Request Payload**
orderId=58&driverId=20&form_key=AKlwzR4oQvfmyhzL

Error

{"message":"Decoding error: \nUnable to unserialize value.\n#0 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/framework/Webapi/Rest/Request/Deserializer/Json.php(64): Magento\Framework\Serialize\Serializer\Json->unserialize('orderId=58&driv...')\n#1 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/framework/Webapi/Rest/Request.php(141): Magento\Framework\Webapi\Rest\Request\Deserializer\Json->deserialize('orderId=58&driv...')\n#2 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/framework/Webapi/Rest/Request.php(199): Magento\Framework\Webapi\Rest\Request->getBodyParams()\n#3 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php(97): Magento\Framework\Webapi\Rest\Request->getRequestData()\n#4 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/module-webapi/Controller/Rest.php(322): Magento\Webapi\Controller\Rest\InputParamsResolver->resolve()\n#5 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/module-webapi/Controller/Rest.php(239): Magento\Webapi\Controller\Rest->processApiRequest()\n#6 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))\n#7 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Webapi\Controller\Rest\Interceptor->___callParent('dispatch', Array)\n#8 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Webapi\Controller\Rest\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))\n#9 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(39): Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch', Array, Array)\n#10 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))\n#11 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/vendor/magento/framework/App/Bootstrap.php(256): Magento\Framework\App\Http->launch()\n#12 /home/dev/Work/Web_Application/SVN/driver-mage/esh_www/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))\n#13 {main}","trace":null}

3
<script>
    require(['jquery'], function($){            
        $.ajax({
            type: "POST",
            url: "http://127.0.0.1/m225/rest/V1/integration/admin/token",
            data: JSON.stringify({'username': 'admin', 'password': 'admin123'}),
            dataType: 'json',
            contentType: 'application/json',
            processData: false,
            success: function(response){
                console.log(response);
                console.log(JSON.stringify(response));
                document.write(JSON.stringify(response));
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        });
    });
</script>

Please change data: data, with data: JSON.stringify(data),

Your Answer

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

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