如何使用wix发出http请求?

How to make http request using wix?

我有一个 Wix 网站,我正在尝试向移动应用发出 API 请求 我有一个名为 Stores 的数据库,其中有一个名为 Products

的集合

我的问题是如何发出获取请求以获取产品集合中的所有值? 我下面的代码不工作

http-functions.js

import {ok, notFound, serverError} from 'wix-http-functions';
import wixData from 'wix-data';


export function get_example(request) {
    const response = {
        "headers": {
            "Content-Type": "application/json"
        }
    };

    return wixData.query("Products")
    .find()
    .then(
        result=> {
            response.body = {
            "items": result.items
        };
        return ok(response);
        }
    )
}

我收到这个错误

{"error":{"name":"Error","errorGroup":"User","code":"WD_VALIDATION_ERROR"}}

我正在尝试向移动应用发出 API 请求

您正在尝试 API 请求 TO 移动应用程序?如果是这样,你需要使用wix-fetch。

如果您尝试 移动应用程序TO 您的 Wix 网站发出请求,那么您就走在了正确的轨道上。您需要拨打如下电话。

import {ok, notFound, serverError} from 'wix-http-functions';
import wixData from 'wix-data';

export function get_getNewItems(request) {
    let options = {
    "headers": {
        "Content-Type": "application/json"
        }
    };
    return getItems(options)
    .catch( (error) => {
        options.body = {
            "origin": "server error",
            "error": error
        };
        return serverError(options);
    });
}

function getItems(options) {
    return wixData.query("Stores/Products")
    .find()
    .then( (results) => {
      // matching items were found
        if(results.items.length > 0) {
            options.body = {
                "origin": "success",
                "items": results.items
            };
            return ok(options);
        } else {
        // no matching items found
            options.body = {
                "origin": "no items",
                "error": 'No items found'
            };
            return notFound(options);
        }
        });
}