Cocos Creator 1.7 cocos数据传递到h5页面上,evaluateJS如何使用

Cocos Creator 1.7以上(含),如何把cocos数据传递到h5页面上,evaluateJS通过这个函数去执行js函数没有任何反应?我的我打开也页面地主(这个页面应许跨域访问):http://116.62.144.162:8020/ ,我对跨域这个东西,没有接触过,我现在在服务段应许跨域访问。不知道如何解决这个问题!
我在iOS平台调试,执行这个this.webView.evaluateJS(‘onClick1(’+ss+’)’);没有任何日志输出,我把我测试工程放到房间里面,帮我看看吧,谢谢!TestWebview.zip (266.8 KB)

cc.Class({
extends: cc.Component,

properties: {
    webView: {
        default: null,
        type: cc.WebView,
    },
},

// use this for initialization
onLoad: function () {

    this.webView.evaluateJS('onClick()');
},

// called every frame
update: function (dt) {

},

onWebFinishLoad: function(sender, event) {
    console.log("onWebFinishLoad == =" + sender);
    var loadStatus = "";
    if (event === cc.WebView.EventType.LOADED) {
        var ss = "sss565454";
        this.webView.evaluateJS('onClick1('+ss+')');
        loadStatus = " is loaded!";
    } else if (event === cc.WebView.EventType.LOADING) {
        loadStatus = " is loading!";
    } else if (event === cc.WebView.EventType.ERROR) {
        loadStatus = ' load error!';
    }
    console.log("onWebFinishLoad=" + loadStatus);
},

btnAction:function(){
    console.log("btnAction start");
    var ss = "sss5654544565655";
    this.webView.evaluateJS('onClick1('+ss+')');
    console.log("btnAction end");
},

});
主要测试代码

这个跨域问题,虽然浏览器的问题,但是不提供响应的解决方案,只会开发者陷入其中

我现在为了规避这个问题,只能不是使用evaluateJS,使用
this.webView,里面节点,发送postMessage,通知h5页面数据
this.webView._sgNode._renderCmd._iframe.contentWindow.postMessage(“9876543210”,"*");,

1赞

这样做native 有问题

native没有跨域的问题,如果使用html5 版本才有跨域的问题

楼主解决问题没有?能分享下不?

楼主解决了么? 我也遇到了这个问题

native 上用

  1. setJavascriptInterfaceScheme 和 setOnJSCallback
  2. evaluateJS

web上用
postMessage

你好 native 上使用evaluateJS 如果带上参数就无响应

你好 native 上使用evaluateJS 如果带上参数就无响应

你好 你的问题解决了嘛?

//重写引擎里面的evaluateJS代码,不然会报错
if (CC_JSB) {
cc.WebView.Impl.prototype.evaluateJS = function (str) {
var iframe = this._iframe;
if (iframe) {
return iframe.evaluateJS(str);
}
}
}

var ss = “sss565454”
this.webView.evaluateJS(‘onClick1(’+ss+’)’) 你好 这样加上参数 无任何反应
有解决方案么?
不带参数就没问题,this.webView.evaluateJS(‘onClick()’)
可以执行

var ss = “sss565454”
this.webView.evaluateJS(‘onClick1(’+ss+’)’)
你好 这样加上参数 无任何反应
有解决方案么?
不带参数就没问题,this.webView.evaluateJS(‘onClick()’)
可以执行

你试试把参数用ss=json.stringify(ss)转换一下数据

我不加参数也没有任何反应。。。。this.operateWeb.evaluateJS(‘setContent()’);我看文档就只用这样就行了吗

我这样可以使用
let token:string = localDB.get(‘token’);
token = JSON.stringify(token);
this.webview.evaluateJS(‘vm.publicFun(’ + token + ‘)’);