Announcement

Collapse
No announcement yet.

Bad Server Response

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bad Server Response

    Hi, I am trying to execute an ajax call, but I keep getting "Bad server response". I am trying to get a list of ID's and put them into a separate text file

    Here is my ajax call:
    Code:
                var id = value.substring(value.indexOf('id=')+3, value.length-1);
    
                $.ajax({
                    type: "POST",
                    url: "/custom/idList.php",
                    data: {"str": id},
                    success: function(){
                    console.log("success");
                    }
                });

    Here is my PHP file under \api\v1\custom
    Code:
    if(isset($_POST["str"])){
        $id = $_POST["str"];
        file_put_contents('path.txt', $id);
    }
    What am I doing incorrectly?

  • #2
    Hi,
    first give full code and the path.
    Why /custom ? why Api ?

    Here sample :

    $.ajax({
    url: 'Contact/action/test',
    type: 'POST',
    data: JSON.stringify({
    id: this.model.id
    }),

    success: function (data) {
    if (data){
    Espo.Ui.notify("Done", 'success', 1000);
    }else{
    Espo.Ui.notify("Error", 'error', 1000);
    }
    }
    });

    And then put controller and get ID

    Regards. (I am not a specialist but it's work)

    Comment


    • #3
      Hello,
      I put it the PHP code in /api/v1/custom because the url path is /espo/api/v1/ + whatever is in the url
      What is the path to Contact/action/test?

      Here is the full code JS code for the action in /espo/client/custom/src/views/test/record

      Code:
      actionGenerateReport: function (data) {
                  var parentid = data.id;
      
      
                  if (!parentid) {
                      return;
                  }
                  var model = this.collection.get(parentid);
                  if (!model) {
                      return;
                  }
      
              //Find attachments and append the links to an array
              var attachments = this.$el.find('[data-id=' + parentid + ']').find('[data-name=capaAttachments]');
              var links = [];
              $(attachments).children().each(function() {
              links.push($(this).find('a').attr('href'));
              });
              $.each(links, function(index, value){
              var path = window.location.hostname + '/espo' + value;
      
                //Sends ids to php to write into a file
                  var id = value.substring(value.indexOf('id=')+3, value.length-1);
      
                  $.ajax({
                      type: "POST",
                      url: "custom/idList.php",
                      data: {"str": id},
                      success: function(data){
                          if(data){
                          console.log("success");
                          }else{
                          console.log("fail");
                          }
                     }
              });
      });
      and the PHP code is in espo/api/v1/custom/idList.php
      Code:
      <?php
      
      
      if(isset($_POST["str"])){
          $id = $_POST["str"];
          file_put_contents('id.txt', $id);
      }else{
      echo "fail";
      }
      exec('script.bat');
      
      ?>
      Running the code shows Bad server response: fail, which I am assuming is because $_POST is not set. Can you also post a sample PHP?

      Comment


      • #4
        Nice,
        put a file in :

        Ajax Call so : I think you have put a a button : then this on your detail.js
        $.ajax({
        url: 'YOURENTITY/action/GenerateReport',
        type: 'POST',
        data: JSON.stringify({
        id: this.model.id
        }),

        success: function (data) {
        if (data){
        Espo.Ui.notify("Done", 'success', 1000);
        }else{
        Espo.Ui.notify("Error", 'error', 1000);
        }
        }
        });

        in : custom/espo/custom/controllers/YOUR_ENTITY.php

        <?php

        namespace Espo\Custom\Controllers;

        class YOUR_ENTITY extends \Espo\Core\Templates\Controllers\TYPE_OF_ENTITY like Person.. like .. ?
        {
        public function actionGenerateReport($params, $data, $request)
        {
        HERE FIND YOUR $params/ data/ request ..
        $data->id =>> id (see up id: this.model.id )

        }
        }

        Comment


        • #5
          Hi

          Yes, it is a button in record/detail.js

          I replaced the ajax call so that it is the same as the what you have (except YOURENTITY has been replaced)

          In custom/espo/custom/controllers/test.php, I have:

          Code:
          <?php
          
          namespace Espo\Custom\Controllers;
          
          class test extends \Espo\Core\Templates\Controllers\BasePlus
          {
              public function postActionGenerateReport($params, $data)
              {
                  if (empty($data->id)) {
                      throw new BadRequest();
                  }
          
                  return $data->id;
              }
          }
          Which just returns the ID back, as a test, however, the "Bad server response" notification is still popping up. The console says "Bad server response: 5c40d2768b4e3634b", which is the id. I have tried actionGenerateReport and postActionGenerateReport, both of them do not work.
          Is there anything else I should double check for errors?

          Comment


          • #6
            Not really understand why you add Post or change NameOfEntity .. but why you change what I have posted ?
            I have send my working sample... espoCRM work with JSON..

            PHP Code:
              <?php  namespace Espo\Custom\Controllers;  class NameOfYourEntity extends \Espo\Core\Templates\Controllers\BasePlus {     public function actionGenerateReport($params$data$request)     {         if (empty($data->id)) {             throw new BadRequest();         }         console.log($data->id);         return $data;     } }

            Comment


            • VinhPN
              VinhPN commented
              Editing a comment
              Thanks a lot! The code started working the other day. Couldn't have done it without your help

          • #7
            Always .. clear cache/ rebuild. and after .. refresh page on browser (clear cache)

            Comment


            • telecastg
              telecastg commented
              Editing a comment
              Excellent explanation, helped me develop my own custom ajax call. Thanks for taking the time to explain every step and for posting the code.
          Working...
          X