简体   繁体   English

使用Javascript发送HTTP请求时出错

[英]Error while sending http request in Javascript

I have a decoupled front-end and back-end. 我的前端和后端分离。 When I press submit button in front-end I would like to send POST http request to the server. 当我在前端按Submit按钮时,我想将POST http请求发送到服务器。

Code for submit: 提交代码:

$scope.onSave = function () {
        $http.post('http://localhost:3000/documents', { user: $scope.user, formData: $scope.formData }, function (response) {
            $scope.isSaved = true;
            console.log(response);
            if (response.success) {
                console.log("It has been successfully saved!")
            }
        });
}

Then, I get an error saying 然后,我得到一个错误说

XMLHttpRequest cannot load http://localhost:3000/documents . XMLHttpRequest无法加载http:// localhost:3000 / documents Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:3001 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:3001 '。 The response had HTTP status code 404. 响应的HTTP状态码为404。

What is an error here? 这是什么错误? I cannot really figure it out. 我真的无法弄清楚。

Another question: Let's say I'm logged in to this website. 另一个问题:假设我已登录该网站。 How do I get a USER info(like username or email) from the website. 如何从网站获取用户信息(如用户名或电子邮件)。 Do I access a URL header to get this info? 我是否要访问URL标头以获取此信息? When I send POST request, I need to send USER_ID as a param to successfully post data to the server. 发送POST请求时,需要发送USER_ID作为参数,才能成功将数据发布到服务器。

您应该允许在服务器端( CORS )进行跨域访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM