Instead of using
$('shopping-cart-totals-table').update(mydata);
Try using
$('shopping-cart-totals-table').update(mydata.myresponse);
You are sending JSON with the key myresponse and your cart block html as the value for that key. So mydata will be an Object after json is converted into a JS object and this object should have a property myresponse with the HTML for the cart block.
Look how Magento 2 handles api response..
class Magento\Framework\Webapi\ServiceOutputProcessor {
...
public function convertValue($data, $type)
{
if (is_array($data)) {
$result = [];
$arrayElementType = substr($type, 0, -2);
foreach ($data as $datum) {
if (is_object($datum)) {
$datum = $this->processDataObject(
$this->dataObjectProcessor->buildOutputDataArray($datum, $arrayElementType)
);
}
$result[] = $datum;
}
return $result;
} elseif ...
}
Magento 2 accept array type to response.
$arr = [
['name' => 'MasterCard'],
['name' => 'VISA'],
['name' => 'DBBL-NEXUS'],
['name' => 'American Express'],
];
return $arr;
or just..
$arr = '[{"name":"MasterCard"},{"name":"VISA"}, {"name":"DBBL-NEXUS"}, {"name":"American Express"}]';
return json_decode($arr, true);
Best Answer
Below are the custom api module with key pair value
To get JSON response, in client set Response headers to
"Content-Type: application/json; charset=utf-8"
and if you need key pair value that is in response should have key and value as like
/rest/V1/categories
we need to create data interfaceTo test in Chrome download plugin called rest client app call the url
below module url will be http://yourdomein.com/magento2/rest/V1/getinfo
app\code\Sugarcode\Customapi\registration.php:
app\code\Sugarcode\Customapi\etc\module.xml:
app\code\Sugarcode\Customapi\etc\di.xml:
app\code\Sugarcode\Customapi\etc\webapi.xml:
app\code\Sugarcode\Customapi\Api\TestInterface.php:
don't remove comments its most important
app\code\Sugarcode\Customapi\Api\Data\TestdataInterface.php (set and get data):
app\code\Sugarcode\Customapi\Model\Test.php:
app\code\Sugarcode\Customapi\Model\Testmodel.php: