【问题标题】:jQuery undefined object error when initialisation code is skipped for some odd reason由于某种奇怪的原因跳过初始化代码时出现jQuery未定义对象错误
【发布时间】:2015-09-18 11:07:35
【问题描述】:

我正在尝试在我的网站上运行 Skype SDK,这将允许我最初登录 Skype。我正在使用的代码来自https://msdn.microsoft.com/EN-US/library/dn962162(v=office.16).aspx,但是当通过它运行 javascript 时会抱怨一个未定义的对象。这是javascript代码(忽略$j,这是我们运行jQuery所需要的),

/**
 * This script demonstrates how to sign the user in and how to sign it out.
 */
$j(function () {
  'use strict';    // create an instance of the Application object;
  // note, that different instances of Application may
  // represent different users
  var Application
  var client;

  Skype.initialize({
    apiKey: 'SWX-BUILD-SDK',
  }, function (api) {
    Application = api.application;
    client = new Application();
  }, function (err) {
    alert('some error occurred: ' + err);
  });

  // whenever state changes, display its value
  client.signInManager.state.changed(function (state) {
    $j('#application_state').text(state);
  });

  // when the user clicks on the "Sign In" button
  $j('#signin').click(function () {
    // start signing in
    client.signInManager.signIn({
      username: $j('#username').text(),
      password: $j('#password').text()
    }).then(
      //onSuccess callback
      function () {
        // when the sign in operation succeeds display the user name
        alert('Signed in as ' + client.personsAndGroupsManager.mePerson.displayName());
      },
      //onFailure callback
      function (error) {
        // if something goes wrong in either of the steps above,
        // display the error message
        alert(error || 'Cannot sign in');
      });
  });

  // when the user clicks on the "Sign Out" button
  $j('#signout').click(function () {
    // start signing out
    client.signInManager.signOut()
      .then(
      //onSuccess callback
      function () {
        // and report the success
        alert('Signed out');
      },
      //onFailure callback
      function (error) {
        // or a failure
        alert(error || 'Cannot sign in');
      });
  });
});

当我运行它时,它不会进入“Skype.initialize({”代码而是跳转到“client.signInManager.state.changed(function (state){”,这是它抛出这个错误的时候“未捕获的TypeError:无法读取未定义的属性'signInManager'”。当通过chrome中的源调试器运行它时,它显示“应用程序”未定义,“客户端”也未定义。所以我的问题是为什么不是这2个在 Skype.initialize 代码中初始化对象?

【问题讨论】:

    标签: javascript jquery skype skype4com


    【解决方案1】:

    您必须在客户端初始化后添加监听器。所以在初始化“成功”回调。您目前实现它的方式是,脚本尝试在客户端初始化之前添加侦听器,在未定义的对象属性上,因此出现错误。

    【讨论】:

    • 谢谢,但我是 jQuery 新手,请您详细说明一下吗?
    猜你喜欢
    • 2022-01-20
    • 2013-01-15
    • 2014-11-22
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多