首页 > 解决方案 > 从 wix 访问 3rd 方 API

问题描述

我正在尝试与第 3 方 API 进行通信。我用python编写了API。我想使用用户表单和文本框从 Wix 网页更新数据库中的名称列。数据库更新和所有端点都使用邮递员进行响应测试。我认为问题在于我在 Wix 端的 JavaScript。

我在以下位置对 Wix 示例中的 JavaScript 进行了建模:https: //support.wix.com/en/article/calling-server-side-code-from-the-front-end-with-web-modules

我有一个名为 placeOrder 的后端模块存储在 orderplaced.jsw 中,它应该将变量“名称”发布到 api。

import { fetch } from 'wix-fetch';
// wix-fetch is the API we provide to make https calls in the backend

export function placeOrder(name) {
 return fetch("https://reliableeparts.pythonanywhere.com/user", {
        method: 'post',
        name: JSON.stringify({ name })

    }).then(function (response) {
 if (response.status >= 200 && response.status < 300){
            console.log(JSON.stringify({ name }))
 return response.text();}

    console.log(Error(response.statusText))



 return Error(response.statusText);}

    );
}

前端模块等待按钮单击并将文本框存储在名称变量中。

{
import {placeOrder} from 'backend/orderplaced.jsw';

export function button1_click(event, $w) {
     placeOrder(
         $w("#input1").value)

         .then(function() {
            console.log("Form submitted to backend.");
        }
     );
}
}

输出: 输出2 代码似乎到达了后端。我相信问题出在我的 placeOrder 函数上,因为我对 JavaScript 不是很熟悉。

标签: javascriptvelo

解决方案


您的代码似乎合法。问题出在服务器上。POST当我尝试向该地址发送请求时,我得到了一个500 Internal Server Error.

您可以检查curl并自己测试服务:

curl -i -X POST -H "Content-Type:application/json" https://reliableeparts.pythonanywhere.com/user -d '{"name":"test123"}'

您可能缺少服务器期望的正确对象结构或缺少服务器的正确标头POST(或两者都...)

确保您遵循此服务器允许的 API


推荐阅读