SugarCRM:如何在 REST 端点中获取 json 数据
SugarCRM: How to get json data in REST endpoint
我在记录列表视图中添加了一个额外的操作;
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 数据(所选 ID 的字符串化数组);
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 数据?
我也做了同样的事情,但我的其余 api 编码在 java 中。我使用 java @Path 注释来注释我的 get 方法。然后我将上面的 rest api 代码部署到服务器(Tomcat 在我的例子中)。启动服务器然后点击由@Path 形成的 URL 将在浏览器上为您提供 json 数据。
解决方案是将调用方法更改为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 请求中被忽略。
我在记录列表视图中添加了一个额外的操作;
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 数据(所选 ID 的字符串化数组);
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 数据?
我也做了同样的事情,但我的其余 api 编码在 java 中。我使用 java @Path 注释来注释我的 get 方法。然后我将上面的 rest api 代码部署到服务器(Tomcat 在我的例子中)。启动服务器然后点击由@Path 形成的 URL 将在浏览器上为您提供 json 数据。
解决方案是将调用方法更改为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 请求中被忽略。