Magento2 AJAX – Use
Related Solutions
$( '#cartqty' ).html('<?php echo $this->helper('checkout/cart')->getSummaryCount(); ?>'); not ajax call .
Call an ajax
on button click
to new url.
Get data from that New Url.so you need to new Action on exit any controller
or create new controller and get data
.
<script>
$( '.product-add-btn' ).click(function() {
new Ajax.Request(url, {
method:'get',
onLoading:function(){ },
onSuccess: function(transport) {
if (200 == transport.status){
var response = transport.responseText.evalJSON( );
$( '#cartqty' ).html(response.count );
}else{
alert('Something went wrong...');
}
},
onFailure: function() { alert('Something went wrong...'); }
});
});
</script>
Need to create a controller by create new module:
<?php
class Amit_Custommodule_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$this->helper('checkout/cart')->getSummaryCount();
$result=array();
$result['count']=this->helper('checkout/cart')->getSummaryCount();
$this->getResponse()->setHeader('Content-type','application/json',true);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
Full module:
- First of all create module control file Module name as
Amit_Custommodule.xml at app/etc/modules/
.
Code:
<?xml version="1.0"?>
<config>
<modules>
<Amit_Custommodule>
<codePool>community</codePool>
<active>true</active>
</Amit_Custommodule>
</modules>
</config>
- Path Of
config.xml
is app/code/community/Amit/Custommodule/etc
Code:
<?xml version="1.0" ?>
<config>
<modules>
<Amit_Custommodule>
<version>1.0.0</version>
</Amit_Custommodule>
</modules>
<frontend>
<routers>
<custommodule>
<use>standard</use>
<args>
<module>Amit_Custommodule</module>
<frontName>custommodule</frontName>
</args>
</custommodule>
</routers>
</frontend>
</config>
- IndexController.php path is
app/code/community/Amit/Custommodule/controllers/
Code:
<?php
class Amit_Custommodule_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$result=array();
$result['count']=Mage::helper('checkout/cart')->getSummaryCount();
$this->getResponse()->setHeader('Content-type','application/json',true);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
And script:
<script>
$( '.product-add-btn' ).click(function() {
new Ajax.Request('<?php echo $this->getUrl("custommodule/index/index")?>', {
method:'get',
onLoading:function(){ },
onSuccess: function(transport) {
if (200 == transport.status){
var response = transport.responseText.evalJSON( );
$( '#cartqty' ).html(response.count );
}else{
alert('Something went wrong...');
}
},
onFailure: function() { alert('Something went wrong...'); }
});
});
</script>
General usage of "script type"
When using <script type="....">
the browser interprets only what it knows (like text/javascript
for example).
Anything else is ignored.
Basically using a custom type you are adding information to the page without displaying it and without the browser interpreting it and you can later use that information as you want.
How Magento uses this
Magento uses these sections after the page loads.
The code that uses them is located in lib/web/mage/apply/scripts.js
.
I don't understand completely what the file mentioned above does, but there is a comment inside the file that states this:
/**
* Parses 'script' tags with a custom type attribute and moves it's data
* to a 'data-mage-init' attribute of an element found by provided selector.
* Note: All found script nodes will be removed from DOM.
*
* @returns {Array} An array of components not assigned to the specific element.
*
* @example Sample declaration.
* <script type="text/x-magento-init">
* {
* "body": {
* "path/to/component": {"foo": "bar"}
* }
* }
* </script>
*
* @example Providing data without selector.
* {
* "*": {
* "path/to/component": {"bar": "baz"}
* }
* }
*/
Conclusion / Speculation
I speculate that this is a way to set different js behaviour to different elements in the page without the need to rewrite the template that contains the elements.
You only need to add a <script type="text/x-magento-init">
in one of your templates, include your template in the page and magento "auto-magically" moves the behavior to the right element.
Related Solutions
$( '#cartqty' ).html('<?php echo $this->helper('checkout/cart')->getSummaryCount(); ?>'); not ajax call .
Call
an ajax
onbutton click
to new url.Get data from that New Url.so you need to
new Action on exit any controller
orcreate new controller and get data
.
<script>
$( '.product-add-btn' ).click(function() {
new Ajax.Request(url, {
method:'get',
onLoading:function(){ },
onSuccess: function(transport) {
if (200 == transport.status){
var response = transport.responseText.evalJSON( );
$( '#cartqty' ).html(response.count );
}else{
alert('Something went wrong...');
}
},
onFailure: function() { alert('Something went wrong...'); }
});
});
</script>
Need to create a controller by create new module:
<?php
class Amit_Custommodule_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$this->helper('checkout/cart')->getSummaryCount();
$result=array();
$result['count']=this->helper('checkout/cart')->getSummaryCount();
$this->getResponse()->setHeader('Content-type','application/json',true);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
Full module:
- First of all create module control file Module name as
Amit_Custommodule.xml at app/etc/modules/
.
Code:
<?xml version="1.0"?>
<config>
<modules>
<Amit_Custommodule>
<codePool>community</codePool>
<active>true</active>
</Amit_Custommodule>
</modules>
</config>
- Path Of
config.xml
isapp/code/community/Amit/Custommodule/etc
Code:
<?xml version="1.0" ?>
<config>
<modules>
<Amit_Custommodule>
<version>1.0.0</version>
</Amit_Custommodule>
</modules>
<frontend>
<routers>
<custommodule>
<use>standard</use>
<args>
<module>Amit_Custommodule</module>
<frontName>custommodule</frontName>
</args>
</custommodule>
</routers>
</frontend>
</config>
- IndexController.php path is
app/code/community/Amit/Custommodule/controllers/
Code:
<?php
class Amit_Custommodule_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$result=array();
$result['count']=Mage::helper('checkout/cart')->getSummaryCount();
$this->getResponse()->setHeader('Content-type','application/json',true);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
And script:
<script>
$( '.product-add-btn' ).click(function() {
new Ajax.Request('<?php echo $this->getUrl("custommodule/index/index")?>', {
method:'get',
onLoading:function(){ },
onSuccess: function(transport) {
if (200 == transport.status){
var response = transport.responseText.evalJSON( );
$( '#cartqty' ).html(response.count );
}else{
alert('Something went wrong...');
}
},
onFailure: function() { alert('Something went wrong...'); }
});
});
</script>
General usage of "script type"
When using <script type="....">
the browser interprets only what it knows (like text/javascript
for example).
Anything else is ignored.
Basically using a custom type you are adding information to the page without displaying it and without the browser interpreting it and you can later use that information as you want.
How Magento uses this
Magento uses these sections after the page loads.
The code that uses them is located in lib/web/mage/apply/scripts.js
.
I don't understand completely what the file mentioned above does, but there is a comment inside the file that states this:
/**
* Parses 'script' tags with a custom type attribute and moves it's data
* to a 'data-mage-init' attribute of an element found by provided selector.
* Note: All found script nodes will be removed from DOM.
*
* @returns {Array} An array of components not assigned to the specific element.
*
* @example Sample declaration.
* <script type="text/x-magento-init">
* {
* "body": {
* "path/to/component": {"foo": "bar"}
* }
* }
* </script>
*
* @example Providing data without selector.
* {
* "*": {
* "path/to/component": {"bar": "baz"}
* }
* }
*/
Conclusion / Speculation
I speculate that this is a way to set different js behaviour to different elements in the page without the need to rewrite the template that contains the elements.
You only need to add a <script type="text/x-magento-init">
in one of your templates, include your template in the page and magento "auto-magically" moves the behavior to the right element.
Best Answer
It is very easy to solve.
Look in this line: https://github.com/magento/magento2/blob/develop/lib/web/mage/mage.js#L93
Here mage waiting when you insert html content with ajax: "text/x-magento-init".
Example:
you insert to
After call magento: