How to pass image data using Ajax to server side in backend magento 2?

By default Image data are not post using ajax. only plain data will be passed as Ajax post. if we need to pass image data using ajax we need to call ajax as below way,
Call jquery using requirejs way in backend template file,
Let’s create a simple example of the form,

<form id="abcd" action="" enctype="multipart/form-data">
	<div class="control">
	    <input type='file' class='multi' name='attachment' id='attachment'/>
	</div>
	<button type="button" name="submitajax" id="submitajax"></button> 
</form>
<script>
    require(["jquery"],function($) {
        $("#submitajax").on('click', function() {
            var file = document.getElementById('attachment').files[0];
            var form;
            if (typeof file != 'undefined') {
                form = new FormData(document.getElementById('attachment'));
                form.append('form_key',window.FORM_KEY);
                form.append('attachment', file);
                form.append('id', getId);
            }
            $.ajax({
                url: "<?= $block->getUrl('myajax/custom/urlname'); ?>",
                type: 'post',
                mimeTypes: 'multipart/form-data',
                data: form,
                cache: false,
                contentType: false,  //this is must required
                processData: false, //this is must required
                beforeSend: function() {
                    $('body').trigger('processStart');
                },
                success: function (response) {
                    console.log(response);
                    $('body').trigger('processStop');
                }
            });
        });
    });
</script>

We need to Use You FormData() object of Javascript to store our image data and using above way we can just send image data to server side using ajax.

How to add external CSS and JS Url in Magento2?

You can add External third-party JS,CSS, and fonts URL using Magento 2 by XML at below way, All the Url will be staying in  <head> tag. Pass Second attribute as  src_type=”url”

For set third-party JS link,

<head>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" src_type="url" />
</head>

For set third-party CSS link,

<head>
	<css src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css" src_type="url" />
</head>

For set font link,

<head>
	<link src="http://fonts.googleapis.com/css?family=Montserrat" src_type="url" />
</head>

For Remove link from the page,  If you want to remove specific JS or CSS from the specific page you can use <remove> tag under the <head> to remove from a specific page.

<head>
    <remove src="ttps://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" />
    <remove src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css" />
    <remove src="http://fonts.googleapis.com/css?family=Montserrat" />
</head>

 

How to cancel order programmatically in magento 2?

In Magento 2 we can cancel an order using below way, Pass Magento/Sales/Api/OrderManagementInterface as DI to __construct() function. We can use Interface method to cancel the order in Magento 2.

Below code snippets is used for cancel order.

<?php

public function __construct(
    \Magento\Sales\Api\OrderManagementInterface $orderManagement
) {
    $this->orderManagement = $orderManagement;
}
/**
 * int $orderId
 * Order cancel by order id $orderId 
 */
public function cancelOrder($orderId) {
    try {
        $this->orderManagement->cancel($orderId);
        return __('You canceled the order successfully.');
    } catch (\Exception $e) {
        return __('You have not canceled the order.');
    }
}

Call function like below from template or any PHP file, $orderId Must be an integer.

$orderId = 1;
$this->cacelOrder($orderId);

The result will be getting based on order success like, You canceled the order successfully otherwise result will be like for error, You have not canceled the order.