1

I'm trying to to an ajax call using following code

$.ajax({
                    type: 'POST'
                    ,url: SOME_URL
                    ,data: {testdata : testid}
                    ,success: function(result){
                     //code
                    }});

SOME_URL points to a controller file having following code.

public function execute()
{

echo '{"test":["-1.00","-2.00"]}';

}

when I debug the success function of ajax call I get data in the result variable of success function parameter.

"{"test":["-1.00","-2.00"]}exception 'Exception' with message 'Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/magentoce21/app/code/<<company>>/<<route_name>>/Controller/Index/View.php:58) in /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php on line 126' in /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/ErrorHandler.php:61
Stack trace:
#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(2, 'Cannot modify h...', '/Applications/M...', 126, Array)
#1 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php(126): setcookie('X-Magento-Vary', '252f89d2ca7ad02...', 0, '/', '', false, true)
#2 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php(78): Magento\Framework\Stdlib\Cookie\PhpCookieManager->setCookie('X-Magento-Vary', '252f89d2ca7ad02...', Array)
#3 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Response/Http.php(85): Magento\Framework\Stdlib\Cookie\PhpCookieManager->setSensitiveCookie('X-Magento-Vary', '252f89d2ca7ad02...', Object(Magento\Framework\Stdlib\Cookie\SensitiveCookieMetadata))
#4 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(37): Magento\Framework\App\Response\Http->sendVary()
#5 /Applications/MAMP/htdocs/magentoce21/vendor/magento/module-page-cache/Model/App/Response/HttpPlugin.php(25): Magento\Framework\App\Response\Http\Interceptor->sendVary()
#6 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/Interception/Interceptor.php(123): Magento\PageCache\Model\App\Response\HttpPlugin->beforeSendResponse(Object(Magento\Framework\App\Response\Http\Interceptor))
#7 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(117): Magento\Framework\App\Response\Http\Interceptor->___callPlugins('sendResponse', Array, Array)
#8 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(185): Magento\Framework\App\Response\Http\Interceptor->sendResponse()
#9 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(156): Magento\Framework\App\Http->handleDeveloperMode(Object(Magento\Framework\App\Bootstrap), Object(Exception))
#10 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Bootstrap.php(263): Magento\Framework\App\Http->catchException(Object(Magento\Framework\App\Bootstrap), Object(Exception))
#11 /Applications/MAMP/htdocs/magentoce21/index.php(52): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#12 {main}"

I checked for any white-spacing in the php code, but found none.

1

Try the following -:

public function execute()  
{      
   echo '{"test":["-1.00","-2.00"]}';
   die();
}
0

Your public function execute() should return 'foobar', not echo 'foobar'.

By echoing it immediately you're throwing everything off hence the headers already sent by (output started

Replace

public function execute()
{

echo '{"test":["-1.00","-2.00"]}';

}

with

public function execute()
{
    return'{"test":["-1.00","-2.00"]}';
}

Or, to be extra fancy:

public function execute()
{
    return json_encode(['test' => [-1, -2]]);
}

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.