【问题标题】:Should I use a web service if I am not performing CRUD operations on DB?如果我不在数据库上执行 CRUD 操作,我应该使用 Web 服务吗?
【发布时间】:2019-06-06 11:33:36
【问题描述】:

我正在编写一个简单的实时搜索块插件。我通过 API 访问外部网站的资源,并在用户键入时显示与用户搜索内容相匹配的结果。 我打算编写一个可以执行此操作的 Web 服务。我会将用户输入作为 ajax.call 中的参数之一传递,然后我的 Web 服务函数将返回建议的结果。 我想知道是否有必要使用网络服务,因为我没有从 Moodle 数据库中检索或返回任何数据,我不想存储建议而只想显示它们。

现在我使用 XMLHttpRequest 在我的插件中调用一个内部 php 文件,该文件通过 api 连接并返回结果, 但我想知道是否有推荐的方法来做到这一点。

//the ajax call
ajax.call([{
    methodname: 'block_xxxx_loadpages',
    args: {userinput: userinput},}])


// the webservice function

class block_xxxx_external extends external_api {

  //parameters
  public static function loadpages() {
    return new external_function_parameters (
      array('userinput' => new external_value(PARAM_TEXT, 'the user input'))
    );
  }

  //the function
  public static function loadpages($userinput = 'userinput') {

    //parameter validation
    $params = self::validate_parameters(self::hello_world_parameters(),
    array('userinput' => $userinput));

    //connect to api and return the result page matching the userinput

    return $result;

  }

  public static function loadpages_returns() {
    return new external_value(PARM_TEXT, 'the result')
  }

}

【问题讨论】:

    标签: php ajax web-services moodle moodle-api


    【解决方案1】:

    除非您很好地处理异常和功能,否则不必为 Moodle 中的每个 ajax 调用提供 Web 服务。 您可以使用常规 XML HttpRequest 来获取数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      相关资源
      最近更新 更多