简体   繁体   English

Netsuite将数据从Portlet POST到JSON中的RESTlet

[英]Netsuite POST data from a Portlet to a RESTlet in JSON

In NetSuite, I'm trying to create a form Portlet POST data to a RESTlet in JSON. 在NetSuite中,我试图创建一个表单Portlet POST数据到JSON中的RESTlet。 I've checked the documentation and internet and all the examples I've been able to find are GET requests or they post to a backend Suitelet instead. 我已经检查了文档和互联网,能够找到的所有示例都是GET请求,或者它们都张贴在后端Suitelet中。

I've come to a point where I can make the request reach the RESTlet but it's not being formatted in JSON, so I get the following error: 我已经到了可以使请求到达RESTlet的地步,但是它没有以JSON格式格式化,因此出现以下错误:

Account: xxxxxxxxxxxxx 帐户:xxxxxxxxxxxxx
Environment: Production Date & Time: 6/11/2015 5:09 pm 环境:制作日期和时间:2015年6月11日5:09 pm
Execution Time: 0.06s 执行时间:0.06s
Script Usage: 0 脚本用法:0
Script: gw_SS_FormBackend 脚本:gw_SS_FormBackend
Type: RESTlet 类型:RESTlet
Function: postWMForm 功能:postWMForm
Error: UNEXPECTED_ERROR 错误:UNEXPECTED_ERROR
SyntaxError: Empty JSON string (null$lib#3) 语法错误:空JSON字符串(null $ lib#3)

I'm using the following code to set the submit button and it's working fine: 我正在使用以下代码来设置“提交”按钮,并且可以正常工作:

var headers = new Array();
headers['User-Agent-x'] = 'SuiteScript-Call';
headers['Authorization'] = 
    'NLAuth nlauth_account=' + cred.account + 
    ', nlauth_email=' + cred.email + 
    ', nlauth_signature=' + cred.password + 
    ', nlauth_role=' + cred.role;
headers['Content-Type'] = 'application/json';
portlet.setSubmitButton(nlapiRequestURL(getRESTletURL(), null, headers, 'POST'), 'Submit', '_hidden');

My problem is I don't know how to convert the form data to JSON before submitting it. 我的问题是我不知道如何在提交表单数据之前将其转换为JSON。

I'd appreciate any help. 我将不胜感激。

Why would you want to use a RESTlet? 您为什么要使用RESTlet? If you are in a portlet then you already have a valid NS session so you'd be better off using a Suitelet. 如果您在portlet中,那么您已经有一个有效的NS会话,因此最好使用Suitelet。 A Suitelet you know is set up to handle JSON would be called thus: 因此,将调用您知道的设置为处理JSON的Suitelet:

nlapiRequestURL(suiteletURL', JSON.stringify{test:'it', when:new Date(), by:'Brett'}), {"content-type":'application/json'}, function(resp){console.log(resp.getBody());}, 'POST');

and your Suitelet code might include something like: 并且您的Suitelet代码可能包含以下内容:

var body = request.getBody();

nlapiLogExecution('DEBUG', 'posted body', body);
var asJSON = JSON.parse(body);

var user = nlapiGetContext().getUser(); // system already knows who this is.
...
var obj = {
   success:true,
   stuff: asProcessed
};

response.setContentType('JAVASCRIPT');
response.writeLine( JSON.stringify(obj) );

Not quite as clean as a RESTlet but you avoid having to hack the credentials. 不像RESTlet那样干净,但是您可以避免破解凭证。

you can use JSON.stringify() function. 您可以使用JSON.stringify()函数。

var headers = new Array();
headers['User-Agent-x'] = 'SuiteScript-Call';
headers['Authorization'] = 
'NLAuth nlauth_account=' + cred.account + 
', nlauth_email=' + cred.email + 
', nlauth_signature=' + cred.password + 
', nlauth_role=' + cred.role;
headers['Content-Type'] = 'application/json';
var myJsonHeader = JSON.stringify(headers);
portlet.setSubmitButton(nlapiRequestURL(getRESTletURL(), null, myJsonHeader, 'POST'), 'Submit', '_hidden');

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM