JavaScript:如何将变量传递给webservice onResult函数?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript:如何将变量传递给webservice onResult函数?相关的知识,希望对你有一定的参考价值。
如果已经提出要求,请原谅我。我不确定用什么关键词来查看这个问题。在我的代码中,我进行了一个webservice调用,并将其结果传递给一个函数。
stockService.GetVideoGet(P.Guid.substring(1), ResponceType).onResult = LoadVideoPlayer;
在函数LoadVideoPlayer中,我查看结果并构建自定义视频播放器。如果视频不存在,那么我会发出警报,说该视频已被禁用。我想做的是.onResult将函数LoadVideoPlayer函数传递给变量P.Guid,这样如果视频不存在,我可以使用它来删除播放器代码。
这是我到目前为止所拥有的。
LoadVideoPlayer = function (result) {
if (result.StatusMessage.toString() === "Success") {
//Build Player using the result information. In the result information is the player ID and I use that ID to refrence the div on the htmlplage to know were the player should be built.
} else {
alert(result.StatusMessage.toString());
//If the player is disabled I dont get a player ID so my player div doesnt know what to do. I am unable to change the webservice results. So I want to try to pass in the P.Guid and use that as the player ID.
}}
有可能做这样的事情
LoadVideoPlayer = function (result, P) {
if (result.StatusMessage.toString() === "Success") {
//Build Player using the result information. In the result information is the player ID and I use that ID to refrence the div on the htmlplage to know were the player should be built.
} else {
alert(result.StatusMessage.toString());
$('#'+P.Guid).innerHTML = 'This player has been disabled.'
}}
答案
弄清楚了。我不得不使用bind并切换我的函数变量。
stockService.GetVideoGet(P.Guid.substring(1), ResponceType).onResult = LoadVideoPlayer.bind(this, P.Guid);
LoadVideoPlayer = function (PG, result) {
if (result.StatusMessage.toString() === "Success") {
//Build Player using the result.
} else {
alert(result.StatusMessage.toString());
alert(PG);
}}
以上是关于JavaScript:如何将变量传递给webservice onResult函数?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Blade 格式 HTML 将 PHP 变量传递给 JavaScript 函数