Magento 2 ajax success json errorI created a custom module ,but getting error, not able to figure out what...

How to approximate rolls for potions of healing using only d6's?

Make me a metasequence

How can atoms be electrically neutral when there is a difference in the positions of the charges?

What can I substitute for soda pop in a sweet pork recipe?

Multiplication via squaring and addition

How to avoid being sexist when trying to employ someone to function in a very sexist environment?

Why does the 31P{1H} NMR spectrum of cis-[Mo(CO)2(dppe)2] show two signals?

Should I choose Itemized or Standard deduction?

Replacement ford fiesta radiator has extra hose

How to add multiple differently colored borders around a node?

Can you benefit from a Hammer of Thunderbolts’s Strength increase by holding it with something else than your hands?

Most significant research articles for practical investors with research perspectives

Why does Starman/Roadster have radial acceleration?

Can you use a beast's innate abilities while polymorphed?

Difference between 小吃 and 零食

Pronunciation of powers

As a new poet, where can I find help from a professional to judge my work?

Where is this triangular-shaped space station from?

A "strange" unit radio astronomy

Logistics of a hovering watercraft in a fantasy setting

How do ISS astronauts "get their stripes"?

I am on the US no-fly list. What can I do in order to be allowed on flights which go through US airspace?

Second-rate spelling

Find the next monthly expiration date



Magento 2 ajax success json error


I created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?PSR Logger not working for Magento 2Magento 2.1.6 Cannot save shipmentError on editing categories in Magento 2Something went wrong while saving the category in Magento 2.1.9Not able to add/update magento2 category post upgrade from 1.9 to 2.1.0Trying to uninstall moduleMagento-2.1 : Warning: Missing argument 2 for OAuthCommonConsumerCredentials::__construct()Magento 2, error 'unserialize value' when using Ajax in phtml in admin pageInvalid Return Type in Ajax Call













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














bumped to the homepage by Community 2 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    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














    bumped to the homepage by Community 2 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      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

      5261023




      5261023





      bumped to the homepage by Community 2 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 2 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          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|improve this answer























              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|improve this answer




























                        0














                        Try the following -:



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





                        share|improve this answer


























                          0












                          0








                          0







                          Try the following -:



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





                          share|improve this answer













                          Try the following -:



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






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 13 at 4:53









                          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

                              “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                              How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                              變成蝙蝠會怎樣? 參考資料 外部連結 导航菜单Thomas Nagel, "What is it like to be a...