Magento 2 ajax success json error












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]: MagentoFrameworkAppErrorHandler->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): MagentoFrameworkStdlibCookiePhpCookieManager->setCookie('X-Magento-Vary', '252f89d2ca7ad02...', Array)
#3 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Response/Http.php(85): MagentoFrameworkStdlibCookiePhpCookieManager->setSensitiveCookie('X-Magento-Vary', '252f89d2ca7ad02...', Object(MagentoFrameworkStdlibCookieSensitiveCookieMetadata))
#4 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(37): MagentoFrameworkAppResponseHttp->sendVary()
#5 /Applications/MAMP/htdocs/magentoce21/vendor/magento/module-page-cache/Model/App/Response/HttpPlugin.php(25): MagentoFrameworkAppResponseHttpInterceptor->sendVary()
#6 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/Interception/Interceptor.php(123): MagentoPageCacheModelAppResponseHttpPlugin->beforeSendResponse(Object(MagentoFrameworkAppResponseHttpInterceptor))
#7 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(117): MagentoFrameworkAppResponseHttpInterceptor->___callPlugins('sendResponse', Array, Array)
#8 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(185): MagentoFrameworkAppResponseHttpInterceptor->sendResponse()
#9 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(156): MagentoFrameworkAppHttp->handleDeveloperMode(Object(MagentoFrameworkAppBootstrap), Object(Exception))
#10 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Bootstrap.php(263): MagentoFrameworkAppHttp->catchException(Object(MagentoFrameworkAppBootstrap), Object(Exception))
#11 /Applications/MAMP/htdocs/magentoce21/index.php(52): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttp))
#12 {main}"


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










share|improve this question



























    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]: MagentoFrameworkAppErrorHandler->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): MagentoFrameworkStdlibCookiePhpCookieManager->setCookie('X-Magento-Vary', '252f89d2ca7ad02...', Array)
    #3 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Response/Http.php(85): MagentoFrameworkStdlibCookiePhpCookieManager->setSensitiveCookie('X-Magento-Vary', '252f89d2ca7ad02...', Object(MagentoFrameworkStdlibCookieSensitiveCookieMetadata))
    #4 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(37): MagentoFrameworkAppResponseHttp->sendVary()
    #5 /Applications/MAMP/htdocs/magentoce21/vendor/magento/module-page-cache/Model/App/Response/HttpPlugin.php(25): MagentoFrameworkAppResponseHttpInterceptor->sendVary()
    #6 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/Interception/Interceptor.php(123): MagentoPageCacheModelAppResponseHttpPlugin->beforeSendResponse(Object(MagentoFrameworkAppResponseHttpInterceptor))
    #7 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(117): MagentoFrameworkAppResponseHttpInterceptor->___callPlugins('sendResponse', Array, Array)
    #8 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(185): MagentoFrameworkAppResponseHttpInterceptor->sendResponse()
    #9 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(156): MagentoFrameworkAppHttp->handleDeveloperMode(Object(MagentoFrameworkAppBootstrap), Object(Exception))
    #10 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Bootstrap.php(263): MagentoFrameworkAppHttp->catchException(Object(MagentoFrameworkAppBootstrap), Object(Exception))
    #11 /Applications/MAMP/htdocs/magentoce21/index.php(52): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttp))
    #12 {main}"


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










    share|improve this question

























      1












      1








      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]: MagentoFrameworkAppErrorHandler->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): MagentoFrameworkStdlibCookiePhpCookieManager->setCookie('X-Magento-Vary', '252f89d2ca7ad02...', Array)
      #3 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Response/Http.php(85): MagentoFrameworkStdlibCookiePhpCookieManager->setSensitiveCookie('X-Magento-Vary', '252f89d2ca7ad02...', Object(MagentoFrameworkStdlibCookieSensitiveCookieMetadata))
      #4 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(37): MagentoFrameworkAppResponseHttp->sendVary()
      #5 /Applications/MAMP/htdocs/magentoce21/vendor/magento/module-page-cache/Model/App/Response/HttpPlugin.php(25): MagentoFrameworkAppResponseHttpInterceptor->sendVary()
      #6 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/Interception/Interceptor.php(123): MagentoPageCacheModelAppResponseHttpPlugin->beforeSendResponse(Object(MagentoFrameworkAppResponseHttpInterceptor))
      #7 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(117): MagentoFrameworkAppResponseHttpInterceptor->___callPlugins('sendResponse', Array, Array)
      #8 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(185): MagentoFrameworkAppResponseHttpInterceptor->sendResponse()
      #9 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(156): MagentoFrameworkAppHttp->handleDeveloperMode(Object(MagentoFrameworkAppBootstrap), Object(Exception))
      #10 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Bootstrap.php(263): MagentoFrameworkAppHttp->catchException(Object(MagentoFrameworkAppBootstrap), Object(Exception))
      #11 /Applications/MAMP/htdocs/magentoce21/index.php(52): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttp))
      #12 {main}"


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










      share|improve this question














      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]: MagentoFrameworkAppErrorHandler->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): MagentoFrameworkStdlibCookiePhpCookieManager->setCookie('X-Magento-Vary', '252f89d2ca7ad02...', Array)
      #3 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Response/Http.php(85): MagentoFrameworkStdlibCookiePhpCookieManager->setSensitiveCookie('X-Magento-Vary', '252f89d2ca7ad02...', Object(MagentoFrameworkStdlibCookieSensitiveCookieMetadata))
      #4 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(37): MagentoFrameworkAppResponseHttp->sendVary()
      #5 /Applications/MAMP/htdocs/magentoce21/vendor/magento/module-page-cache/Model/App/Response/HttpPlugin.php(25): MagentoFrameworkAppResponseHttpInterceptor->sendVary()
      #6 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/Interception/Interceptor.php(123): MagentoPageCacheModelAppResponseHttpPlugin->beforeSendResponse(Object(MagentoFrameworkAppResponseHttpInterceptor))
      #7 /Applications/MAMP/htdocs/magentoce21/var/generation/Magento/Framework/App/Response/Http/Interceptor.php(117): MagentoFrameworkAppResponseHttpInterceptor->___callPlugins('sendResponse', Array, Array)
      #8 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(185): MagentoFrameworkAppResponseHttpInterceptor->sendResponse()
      #9 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Http.php(156): MagentoFrameworkAppHttp->handleDeveloperMode(Object(MagentoFrameworkAppBootstrap), Object(Exception))
      #10 /Applications/MAMP/htdocs/magentoce21/vendor/magento/framework/App/Bootstrap.php(263): MagentoFrameworkAppHttp->catchException(Object(MagentoFrameworkAppBootstrap), Object(Exception))
      #11 /Applications/MAMP/htdocs/magentoce21/index.php(52): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttp))
      #12 {main}"


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







      magento-2.1 ajax header json






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 29 '16 at 10:01









      NausifNausif

      536922




      536922






















          2 Answers
          2






          active

          oldest

          votes


















          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]]);
          }





          share|improve this answer































            0














            Try the following -:



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




            share























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "479"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f152341%2fmagento-2-ajax-success-json-error%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              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]]);
              }





              share|improve this answer




























                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]]);
                }





                share|improve this answer


























                  0












                  0








                  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]]);
                  }





                  share|improve this answer













                  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]]);
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 29 '16 at 12:32









                  EamonnEamonn

                  450210




                  450210

























                      0














                      Try the following -:



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




                      share




























                        0














                        Try the following -:



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




                        share


























                          0












                          0








                          0







                          Try the following -:



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




                          share













                          Try the following -:



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





                          share











                          share


                          share










                          answered 5 mins ago









                          user63323user63323

                          412




                          412






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Magento Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f152341%2fmagento-2-ajax-success-json-error%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              What other Star Trek series did the main TNG cast show up in?

                              Berlina muro

                              Berlina aerponto