gpt4 book ai didi

javascript - Class Cast : java. lang.String 无法转换为 org.mozilla.javascript.Scriptable

转载 作者:行者123 更新时间:2023-11-30 17:30:30 26 4
gpt4 key购买 nike

在调用 http 适配器过程时,它会弹出一个包含过程名称、签名和参数的对话框,当我在输入两个字符串类型参数后点击“运行”按钮时,我得到“Class Cast: java.lang.String cannot be cast to org .mozilla.javascript.Scriptable”错误。

仅供引用,我使用 worklight 应用程序框架数据对象编辑器创建了一个 worklight 适配器(自动生成 .xml 和 impl.js 文件)

impl.js 文件

function CurrencyConvertor_ConversionRate(params, headers){
var soapEnvNS;

soapEnvNS = 'http://schemas.xmlsoap.org/soap/envelope/';
var request = buildBody(params, 'xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://www.webserviceX.NET/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" ', soapEnvNS);
return invokeWebService(request, headers);
}



function buildBody(params, namespaces, soapEnvNS){
var body =
'<soap:Envelope xmlns:soap="' + soapEnvNS + '">\n' +
'<soap:Body>\n';

body = jsonToXml(params, body, namespaces);

body +=
'</soap:Body>\n' +
'</soap:Envelope>\n';
return body;
}

function getAttributes(jsonObj) {
var attrStr = '';
for(var attr in jsonObj) {
var val = jsonObj[attr];
if (attr.charAt(0) == '@') {
attrStr += ' ' + attr.substring(1);
attrStr += '="' + val + '"';
}
}
return attrStr;
}

function jsonToXml(jsonObj, xmlStr, namespaces) {
var toAppend = '';
for(var attr in jsonObj) {
var val = jsonObj[attr];
if (attr.charAt(0) != '@') {
toAppend += "<" + attr;
if (typeof val === 'object') {
toAppend += getAttributes(val);
if (namespaces != null)
toAppend += ' ' + namespaces;
toAppend += ">\n";
toAppend = jsonToXml(val, toAppend);
}
else {
toAppend += ">" + val;
}
toAppend += "</" + attr + ">\n";
}
}
return xmlStr += toAppend;
}


function invokeWebService(body, headers){
var input = {
method : 'post',
returnedContentType : 'xml',
path : '/CurrencyConvertor.asmx',
body: {
content : body.toString(),
contentType : 'text/xml; charset=utf-8'
}
};

//Adding custom HTTP headers if they were provided as parameter to the procedure call
headers && (input['headers'] = headers);

return WL.Server.invokeHttp(input);
}

最佳答案

该错误表明您的代码中某处存在无效的 JSON 对象。

这个错误很可能是在使用 body.toString() 将正文转换为字符串时引发的因为 toString 将返回 [object Object],这是无效的 JSON 对象值(既不是有效的字符串也不是有效的数组)

改用 json.stringify(body),它应该可以实现您的预​​期。

此外,尝试添加一些日志行以方便跟踪错误

关于javascript - Class Cast : java. lang.String 无法转换为 org.mozilla.javascript.Scriptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23192346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com