javascript - 从 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 不是很熟悉。
解决方案
您的代码似乎合法。问题出在服务器上。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
推荐阅读
- javascript - 在 JavaScript 中首先使用 for 循环加载最后一条记录
- nativescript - Nativescript-dev-appium 测试在“检查模拟器是否正在运行”之后卡住了
- ios - 位置发生显着变化后,应用程序不会在后台静默启动
- groovy - 嵌套闭包中的 Groovy resolveStrategy
- reactjs - 我可以在循环中更改单个子组件而不重新渲染整个列表(反应钩子)
- c++ - C ++中的两个连续for循环,第二个循环不起作用
- ios - 无法使用 segue swift 5 将数据从一个控制器传递到另一个控制器
- python-3.x - 使用 Python 在(子)文件夹中搜索特定文件名
- c# - 在 Visual Studio 中设置表视图时出现公共列表错误
- excel - 如果单词多次退出,则从范围中查找单词列表