0

登录时,通过服务发出 AJAX 请求Account.Login.Service.ss。在这个服务中,我也调用了我自己的服务WebStore.Service.ss——这就是我所指的服务间通信。

奇怪的是,这种服务间通信在后端工作,但前端抱怨以下错误:

未捕获的语法错误:位置 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'>&nbsp;</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'>&nbsp;</td></tr>
<tr><td class='text'>&nbsp;</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>

4

1 回答 1

0

您的 Account.Login.Service.ss 仅使用 html 响应,因为 NetSuite 试图告诉您一个错误。通常它也会发送 JSON。// no error occurs您在调用后的代码中发表评论console.log(),但这可能是导致问题的原因。您正在尝试从服务器端服务登录到控制台,所以这不起作用。请改用 nlapiLogExecution()。

于 2017-05-30T17:56:12.563 回答