2016-12-07 6 views
0

Я добавил дополнительное действие в список записей;SugarCRM: как получить данные json в конечной точке REST

custom/modules/Opportunities/clients/base/views/recordlist/recordlist.js:

({ 
    extendsFrom: 'RecordlistView', 

    initialize: function(options) { 
     this._super("initialize", [options]); 
     //add listener for custom button 
     this.context.on('list:opportunitiesexport2:fire', this.export2, this); 
    }, 
    export2: function() { 
     //gets an array of ids of all selected opportunities 
     var selected = this.context.get("mass_collection").pluck('id'); 
     if (selected) { 
      return App.api.call('read', 
      App.api.buildURL('Opportunities/Export2'), 
      {'selected_ids':selected}, 
      { 
       success: function(response) { 
        console.log("SUCCESS"); 
        console.log(response); 
       }, 
       error: function(response) { 
        console.log('ERROR'); 
        console.log(response); 
       }, 
       complete: function(response){ 
        console.log("COMPLETE"); 
        console.log(response); 
       }, 
       error: function(response){ 
        console.log("ERROR"); 
        console.log(response); 
       } 
      }); 
     } 
    }, 
}) 

Учебник здесь http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Integration/Web_Services/v10/Extending_Endpoints/ Объясняет, как создать конечную точку.

Однако он не объясняет, как получить данные json (строковый массив выбранных идентификаторов);

custom/modules/Opportunities/clients/base/api/OpportunitiesApi.php:

class OpportunitiesApi extends SugarApi 
{ 
    public function registerApiRest() 
    { 
     return array(
      //GET 
      'MyGetEndpoint' => array(
       //request type 
       'reqType' => 'GET', 

       //set authentication 
       'noLoginRequired' => false, 

       //endpoint path 
       'path' => array('Opportunities', 'Export2'), 

       //endpoint variables 
       'pathVars' => array('', ''), 

       //method to call 
       'method' => 'Export2', 

       //short help string to be displayed in the help documentation 
       'shortHelp' => 'Export', 

       //long help to be displayed in the help documentation 
       'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html', 
      ), 
     ); 
    } 

    /** 
    * Method to be used for my MyEndpoint/GetExample endpoint 
    */ 
    public function Export2($api, $args) 
    { 
     //how to access $args['selected_ids']? 
    } 
} 

$args содержит

Array 
(
    [__sugar_url] => v10/Opportunities/Export2 
) 

Можно ли получить доступ к данным JSON?

ответ

0

Решение заключалось в том, чтобы изменить метод вызова на create, а метод конечной точки - на POST; $args теперь содержит

Array 
(
    [selected_ids] => Array 
     (
      [0] => 0124a524-accc-11e6-96a8-005056897bc3 
     ) 

    [__sugar_url] => v10/Opportunities/Export2 
) 

PUT vs POST in REST - я использую GET, потому что я не планировал ничего менять, но тело, как правило, игнорируется в запросе GET.

0

Я сделал то же самое, но мой отдых api был закодирован в java. Я использовал аннотацию java @Path для аннотирования метода get. Затем я развернул выше код api на сервере (Tomcat в моем случае). Запуск сервера, а затем попадание URL-адреса, образованного @Path, даст вам данные json в браузере.