我使用Meteor做web应用程序,想法是我们拥有客户端的所有功能,并且客户端会对我们的REST API执行API调用。问题是我不管理向我们的API服务器发送任何HTTPS POST请求。互联网充斥着说CORS头文件存在问题的帖子,但API服务器发送头文件:access-control-allow-origin:*。所以这不应该是问题。使客户端只使用流星的POST请求到第三方API
我有简单的登录模板,该模板我会抓住这个提交事件:
import {Template} from 'meteor/templating';
import { HTTP } from 'meteor/http';
import "./login.html";
Template.login.events({
'submit .login_form'(event) {
const target = event.target;
let username = target.username.value;
let password = target.password.value;
// attempted HTTPS POST request
// Meteor.http.post(my_api_url, { // no success with this either
HTTP.call("POST", my_api_url, {
data: {
method: "login",
params: {
username: username,
passwrd: password
}
}
}, function (error, result) {
// we will end up here just fine, but 'error' contains error
// result is undefined
console.log(error);
});
// do stuff
},
});
的console.log(结果)给了我这个错误:
Error: network
Stack trace:
httpcall_client.js/HTTP.call/[email protected]://localhost:3000/packages/http.js?hash=2a49...181ea:243:20
是的,我已经用邮递员测试了API,它应该返回上面显示的数据。我在本地主机上运行Meteor应用程序。
编辑: 的JSON我试图传递给API是
{"method": "login",
"params": {"username": "username",
"passwrd": "password"}}
编辑2: 当在浏览器的网络选项卡来看,我看不出有什么走出去请求。
我的流星模板看起来是这样的:
<template name="login">
<h1>Login</h1>
<form class="login_form" method="POST">
<input type="text" name="username" placeholder="Username"/>
<input type="password" name="password" placeholder="Password"/>
<input class="btn btn-success" type="submit" value="Log In"/>
</form>
</template>
什么是你的'my_api_url'的价值? – Styx
它是流星的全局变量,字符串。 https://myurl.foo/api。不能透露真实的网址 –
好吧,你可以在做你的请求之前'console.log'这个变量吗?所以你会确定它是正确的。 – Styx