类Cast:java.lang.String无法强制转换为org.mozilla.javascript.Scriptable [英] Class Cast: java.lang.String cannot be cast to org.mozilla.javascript.Scriptable

查看:146
本文介绍了类Cast:java.lang.String无法强制转换为org.mozilla.javascript.Scriptable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用http适配器程序时,会弹出一个带有ProcedureName,Signature和Paramaters的对话框,当我输入两个字符串类型参数后点击Run按钮时,我得到Class Cast:java.lang.String无法强制转换为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/XMLSchemaxmlns: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);
返回invokeWebService(request,headers);
}



函数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';
返回机构;
}

函数getAttributes(jsonObj){
var attrStr ='';
for(jsonObj中的var attr){
var val = jsonObj [attr];
if(attr.charAt(0)=='@'){
attrStr + =''+ attr.substring(1);
attrStr + ='='+ val +'';
}
}
返回attrStr;
}

函数jsonToXml(jsonObj,xmlStr,namespaces){
var toAppend ='';
for(jsonObj中的var attr){
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;
}
}
返回xmlStr + = toAppend;
}


函数invokeWebService(正文,标题){
var input = {
method:'post',
returnedContentType:' xml',
路径:'/ CurrencyConvertor.asmx',
body:{
content:body.toString(),
contentType:'text / xml; charset = utf-8'
}
};

//添加自定义HTTP标头(如果它们作为参数提供给过程调用
headers&& (input ['headers'] = headers);

返回WL.Server.invokeHttp(输入);
}


解决方案

错误表明有代码中某处的无效JSON对象。



使用 body.toString()
as toString 将返回 [object Object] 这是无效的JSON对象值(两者都不是有效的字符串也不是有效的数组)



使用 json.stringify(正文)相反,它应该是你想要的另外,尝试添加一些日志行以便于跟踪错误


While invoking a http adapter procedure, it popsup a dialog with ProcedureName, Signature and Paramaters and when I hit Run button after entering two string type parameters, I am getting "Class Cast: java.lang.String cannot be cast to org.mozilla.javascript.Scriptable" error.

FYI, I created a worklight adapter using worklight application framework data object editor(automatically generates .xml and impl.js files)

impl.js file

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);
}

解决方案

The error indicates that there is an invalid JSON object somewhere in your code.

Most probably this error raised while converting the body to String using body.toString() as toString will return [object Object] which is invalid JSON object value (neither valid String nor valid Array)

use json.stringify(body) instead, it should make what you intended to do.

besides, try to add some log lines to ease tracing the error

这篇关于类Cast:java.lang.String无法强制转换为org.mozilla.javascript.Scriptable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆