首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类强制转换:不能将java.lang.String转换为org.mozilla.javascript.Scriptable

类强制转换:不能将java.lang.String转换为org.mozilla.javascript.Scriptable
EN

Stack Overflow用户
提问于 2014-04-21 14:48:54
回答 1查看 4.1K关注 0票数 1

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

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

impl.js文件

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

发布于 2014-05-01 06:34:26

该错误指示代码中的某个位置存在无效的JSON对象。

在使用body.toString()作为toString将主体转换为字符串时,很可能会出现这个错误,它会返回[object Object],这是无效的JSON对象值(既不是有效的字符串,也不是有效的数组)

改为使用json.stringify(body),它应该会实现您想要做的事情。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23192346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档