简体   繁体   English

前端在 CORS 请求中从后端获取错误 401 - apache/php 配置不足?

[英]Frontend gets Error 401 from Backend in CORS request - apache/php insufficiently configured?

I have a frontend, running on most recent vue on port 5000. Then I have my backend, running on most recent lumen on port 8080. Both are on my localmachine.我有一个前端,在端口 5000 上运行最新的 vue。然后我有我的后端,在端口 8080 上的最新流明上运行。两者都在我的本地机器上。 Later in production, they will be separated on two virtual machines.稍后在生产中,它们将在两个虚拟机上分开。

Right now, when sending an http request from the frontend to the backend, I get the following error to my devConsole (NOT the vue devtools, but the usual firefox/chrome devtools console!):现在,当从前端向后端发送 http 请求时,我的 devConsole 收到以下错误(不是 vue devtools,而是通常的 firefox/chrome devtools 控制台!):

Error: Request failed with status code 401

The function returning the axios object executing the http request looks like this:返回执行 http 请求的 axios 对象的函数如下所示:

function fetchData(method, slug, payload) {
  return axios[method](`${API_ENDPOINT}${slug}`, payload);
}

The call looks like this:调用如下所示:

fetchData(METHOD_POST, USER_LIST, "someToken").then((res)=>{
        return res
      })

METHOD_POST contains the string post and USER_LIST contains the string /user/show_data which is used to further specify the URL. METHOD_POST包含字符串postUSER_LIST包含用于进一步指定 URL 的字符串/user/show_data In my Lumen backend, it accesses the following route:在我的 Lumen 后端,它访问以下路由:

$router->group(['prefix' => 'user', 'middleware' => 'auth'], function() use ($router){

    $router->post('/show_data', 'UserController@getData');

});

The route and the respective controller work, I've tested it with RESTClient https://addons.mozilla.org/de/firefox/addon/restclient/路由和相应的控制器工作,我已经用 RESTClient https://addons.mozilla.org/de/firefox/addon/restclient/对其进行了测试

I think that syntaxwise, everything is fine.我认为在语法上,一切都很好。 Also, the token wasnt expired when I used it, I checked this multiple times.此外,令牌在我使用时没有过期,我多次检查。 So I think that it might be connected to the CORS-Policy of the apache and/or php.所以我认为它可能与apache和/或php的CORS-Policy有关。 Im running apache and php from the latest XAMPP dist.我从最新的 XAMPP 发行版运行 apache 和 php。

Still, I'm not entirely sure if thats it, especially since this is the first time that I connect a backend using lumen and a frontend using vue.js.不过,我不完全确定是否是这样,特别是因为这是我第一次使用 lumen 连接后端和使用 vue.js 连接前端。

EDIT: I managed to activate "withCredentials" for my axios object by changing the function body to:编辑:我通过将函数体更改为:

  axios.defaults.headers.withCredentials = true;
  return axios[method](`${API_ENDPOINT}${slug}`, payload);

However, now the CORS problem turns up ^^ When sending the http request, I get these error messages to my console:但是,现在出现了 CORS 问题 ^^ 发送 http 请求时,我将这些错误消息发送到我的控制台:

Access to XMLHttpRequest at 'http://localhost:8080/user/show_data' from origin 'http://localhost:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

And

xhr.js:178 POST http://localhost:8080/user/show_data net::ERR_FAILED

And

createError.js:16 Uncaught (in promise) Error: Network Error
    at t.exports (createError.js:16)
    at XMLHttpRequest.d.onerror (xhr.js:83)

What should I do?我该怎么办?

EDIT2: I now added the following lines to my httpd.conf and restarted apache and lumenproject: EDIT2:我现在将以下几行添加到我的 httpd.conf 并重新启动 apache 和 lumenproject:

<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Max-Age: 360
Header set Access-Control-Allow-Credentials: true
Header set Access-Control-Allow-Methods: *
Header set Access-Control-Allow-Headers: Origin
Header set Access-Control-Expose-Headers: Access-Control-Allow-Origin
</IfModule>

I also tried out explicitely specifying the origin to我还尝试明确指定原点

http://localhost:8080

and

http://localhost:5000

But it didnt help.但它没有帮助。 The errormessage remains the same.错误消息保持不变。

Most browser will block you anyway if your backend allow any host.如果您的后端允许任何主机,大多数浏览器无论如何都会阻止您。

Access-Control-Allow-Origin "*"

You are also missin a semi-colon你也错过了一个分号

Access-Control-Allow-Origin: "*"

Try尝试

Access-Control-Allow-Origin: "yourorigindomain.com"

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

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