When logging in, an AJAX request is made through the service Account.Login.Service.ss
. In this service I am also calling my own service WebStore.Service.ss
- this is the inter-service communication I refer to.
Weirdly this inter-service communication works backend, but the front-end complains with the following error:
Uncaught SyntaxError: Unexpected token < in JSON at position 0
Screenshot of error:
The response from the original AJAX/XHR request is HTML (which is why the above error is complaining about the <
). See the below snippet for the response from the XHR/AJAX request. If I remove the inter-service communication then everything works fine.
My service WebStore.Service.ss
responds with JSON not HTML so I don't think its the cause.
Any idea what is wrong and how I can get inter-service communication to work?
Details:
Below specifically details the services code:
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);
}
}
Response from 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>
Your Account.Login.Service.ss is only responding with html because there is an error that NetSuite is trying to tell you about. Usually it would send JSON too. You comment inside the code that
// no error occurs
following aconsole.log()
call, but this could be what's causing the problem. You are trying to log to the console from a server side service, so that won't work. Use nlapiLogExecution() instead.