登录时,通过服务Account.Login.Service.ss
发出AJAX请求。在这项服务中,我也在调用我自己的服务WebStore.Service.ss
- 这是我所指的服务间通信。
奇怪的是,这种跨服务通信在后端运行,但前端抱怨以下错误:
未捕获的SyntaxError:意外的令牌<在位置0的JSON中
错误屏幕截图:
原始AJAX / XHR请求的响应是HTML(这就是为什么以上错误抱怨<
)。有关XHR / AJAX请求的响应,请参阅下面的代码段。如果我删除了服务间通信,那么一切正常。
我的服务WebStore.Service.ss
以JSON而不是HTML回应,所以我认为不是原因。
知道什么是错的,以及如何让服务间通信工作?
下面详细介绍服务代码:
Account.Login.Service.ss
// Account.Login.Service.ss
function service (request)
{
'use strict';
var Application = require('Application');
try
{
var method = request.getMethod()
, Account = require('Account.Model')
, data = JSON.parse(request.getBody() || '{}');
// Call service
// Note no error occurs and everything works.
try {
var headers = {"User-Agent-x": "SuiteScript-Call",
"Content-Type": "application/json"};
var response = nlapiRequestURL("http://mywebsite.com/sca-dev-montblanc/services/WebStore.Service.ss?type=is_email_valid&email=foo%40bar.com", null, headers);
console.log("Making Service call", response);
}
catch (ex) {
console.log("Error:", e.message); // no error occurs
}
// END DEBUG
switch (method)
{
case 'POST':
//Handles the login and send the response
Application.sendContent(Account.login(data.email, data.password, data.redirect));
break;
default:
// methodNotAllowedError is defined in ssp library commons.js
Application.sendError(methodNotAllowedError);
}
}
catch (e)
{
Application.sendError(e);
}
}
WebStore.Service.ss
function service(request, response)
{
var Application = require('Application');
try {
if (request.getMethod() == 'GET') {
Application.sendContent( {foo: "bar"} );
}
}
catch (e) {
Application.sendError(e);
}
}
来自http://.../Account.Login.Service.ss?n=2&c=4224945&n=2
<!DOCTYPE html>
<html>
<head>
<title>Notice (SuiteScript)</title>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<script type='text/javascript' src='/ui/jquery/jquery-1.11.3.min.js?NS_VER=2017.1.0&minver=75'></script>
<script type='text/javascript' src='/ui/jquery/jquery_isolation.js?NS_VER=2017.1.0&minver=75'></script>
<script type='text/javascript' src='/javascript/NLUtil.jsp?NS_VER=2017.1.0&minver=75&locale=en_AU&JSP_VER=1&locale=en_AU&JSP_VER=1&JSP_VER=1'></script>
<script type='text/javascript' src='/javascript/NLUtil.js?NS_VER=2017.1.0&minver=75'></script>
<script type='text/javascript' src='/javascript/NLUIWidgets.jsp?NS_VER=2017.1.0&minver=75&locale=en_AU&JSP_VER=1&locale=en_AU&JSP_VER=1&JSP_VER=1'></script>
<script type='text/javascript' src='/assets/legacy_widgets/1701411811.js?NS_VER=2017.1.0&minver=75'></script>
<script type='text/javascript' src='/assets/legacy_apputil/1733410633.js?NS_VER=2017.1.0&minver=75'></script>
<script type='text/javascript' src='/javascript/NLAppUtil.jsp?NS_VER=2017.1.0&minver=75&locale=en_AU&JSP_VER=1&locale=en_AU&JSP_VER=1&JSP_VER=1'></script>
<script type='text/javascript' src='/uirefresh/script/global.js__NS_VER=2017.1.0&minver=75&locale=en_AU.nlqs?NS_VER=2017.1.0&minver=75'></script>
<link rel='stylesheet' href='/core/styles/pagestyles.nl'><link rel='stylesheet' type='text/css' href='/uirefresh/css/button.css' />
</head>
<body bgcolor='#FFFFFF' link='#000000' vlink='#000000' alink='#330099' text='#000000' topmargin='0' marginheight='1' onload='page_init()' class='error-page'>
<img src='/images/nav/stretch.gif' width='10'><img class='uir-logo' src='/images/logos/netsuite-reskin.png' border=0 style='margin-right:30px;'><img src='/images/logos/sandbox_logo.gif' height='30' width='125' border=0>
<table border=0 cellPadding=0 cellSpacing=0 width=100%>
<tr><td class='bglt'>
<table border='0' cellspacing='0' cellpadding='5' width='100%'>
<tr><td class='textboldnolink'>Notice (SuiteScript)</td></tr>
<tr><td vAlign='top'>
<table border='0' cellspacing='0' cellpadding='0' width='100%'>
<tr><td class='text'> </td></tr>
<tr><td class=text><img src='/images/5square.gif' width=5 height=5>An unexpected SuiteScript error has occurred</td></tr>
<tr><td class='text'> </td></tr>
<tr><td class='text'> </td></tr>
</table></td></tr></table></td></tr>
<tr><td> <table id='tbl_goback' cellpadding='0' cellspacing='0' border='0' class='uir-button' style='margin-right:6px;cursor:hand;' role='presentation'> <tr id='tr_goback' class='pgBntG pgBntB'> <td id='tdleftcap_goback'><img src='/images/nav/ns_x.gif' class='bntLT' border='0' height='50%' width='3' alt=''/> <img src='/images/nav/ns_x.gif' class='bntLB' border='0' height='50%' width='3' alt=''/> </td> <td id='tdbody_goback' height='20' valign='top' nowrap class='bntBgB'> <input type='button' style='' class='rndbuttoninpt bntBgT' value='Go Back' id='goback' name='goback' onclick="history.back(); return false;" onmousedown="this.setAttribute('_mousedown','T'); setButtonDown(true, false, this);" onmouseup="this.setAttribute('_mousedown','F'); setButtonDown(false, false, this);" onmouseout="if(this.getAttribute('_mousedown')=='T') setButtonDown(false, false, this);" onmouseover="if(this.getAttribute('_mousedown')=='T') setButtonDown(true, false, this);" ></td> <td id='tdrightcap_goback'> <img src='/images/nav/ns_x.gif' height='50%' class='bntRT' border='0' width='3' alt=''> <img src='/images/nav/ns_x.gif' height='50%' class='bntRB' border='0' width='3' alt=''> </td> </tr> </table> </td></tr>
</table>
<script language='JavaScript' type='text/javascript'>
function page_init() {
}
</script>
</body>
</html>
答案 0 :(得分:0)
您的Account.Login.Service.ss仅响应html,因为NetSuite试图告诉您一个错误。通常它也会发送JSON。您在// no error occurs
调用console.log()
之后的代码内发表评论,但这可能是造成问题的原因。您正尝试从服务器端服务登录到控制台,因此无法正常工作。改为使用nlapiLogExecution()。