Home > Software design >  Pass data from twig templete to controller by ajax call in shopware 6
Pass data from twig templete to controller by ajax call in shopware 6

Time:12-21

Any one guide how I can pass data from twig templete to controller by ajax call in shopware 6.

By using this code I successfully call controller(ajaxcall) by ajax call but I didn't pass data into controller.


import HttpClient from 'src/service/http-client.service';
import Plugin from 'src/plugin-system/plugin.class';

export default class customPlugin extends Plugin {

init() {

    // initalize the HttpClient
    this._client = new HttpClient();
    document.getElementById("button").addEventListener("click", this.displayDate);


}
displayDate() { 
    this._client = new HttpClient();
      // make the network request
    this._client.get('/ajaxcall');
}

}


your guidelines really able to appreciate!

CodePudding user response:

get is no really valid thing to submit data, use post instread...

An example:

this._httpClient.post('/ajaxcall', JSON.stringify({
            mykey: 'myValue',
            mySecondKey: 'mySecondValue',
        }), response => {
            // do something with response 
        });

CodePudding user response:

<script>
var data = new FormData();
data.append('key1',value1);
data.append('key2',value2);

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(response) {
    if (xhr.readyState == XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4
        if (xhr.status == 200) {
            // success
        }
        else if (xhr.status == 400) {
            
        }
        else {
            
        }
    }
};


xhr.open('POST', url, true);
xhr.onload = function () {
    
};
xhr.send(data);
  • Related