本篇文章给大家带来的内容是关于laravel api跨域访问的实现步骤,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
服务器a请求服务器b的接口,那么一般会出现跨域问题。
1
xmlhttprequest cannot load http://api.console.vms3.com/api/ur. no 'access-control-allow-origin' header is prent on the requested resource. origin 'ht角度的换算tp://localhost:8080' istherefore not allowed access.
意思就是服务器响应不允许跨域访问.
那我们就需要让服务器支持跨域访问, 也就是在响应头部中添加
1
'access-control-allow-origin: *'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
创建 `app/http/middleware/accesscontrolalloworigin.php` middleware 把 'access-control-allow-origin: *' 写入头部.
app/http/middleware/成都专科学校排名accesscontrolalloworigin.php
<?php
namespace app\http\middleware;
u closure;
u illuminate\support\facades\auth;
class accesscontrolalloworigin
{
/**
*
* handle an incoming request.
*
* @param \illuminate\http\request $request
* @param \closure $next
* @return mixed
*/
public function handle($request, closure $next)
{
header('access-control-allow-origin: *');
header("access-control-allow-credentials: true");
header("access-control-allow-methods: *");
header("access-control-allow-headers: content-type,access-token");
header("access-control-expo-headers: *");
return $next($request);
}
}
注册这个middleware到kernel中.
分别在protected $middleware数组中和protected $routemiddleware数组中
添加我们刚才创建的那个文件class名, 使用cors这个别名.
然后在设置它保护 api , 就是$middlewaregroups['api']的数组中添加它的别名, 本文中是'cors'app/http/kernel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace app\http;
u illuminate\foundation\http\kernel 4x400米接力as httpkernel;
class kernel extends httpkernel
{
/**
* the application's global http middleware stack.
*
* the middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\illuminate\foundation\http\middleware\checkformaintenancemode::class,
\illuminate\foundation\http\middleware\validatepostsize::class,
\app\http\middleware\trimstrings::class,
\illuminate\foundation\http\middleware\convertemptystringstonull::class,
\app\http\middleware\accesscontrolalloworigin::class,
];
/**
* the application's route middleware groups.
*
* @var array
*/
protected $middlewaregroups = [
'web' => [
\app\http\middleware\encryptcookies::class,
\illuminate\cookie\middleware\addqueuedcookiestorespon::class,
\illuminate\ssion\middleware\startssion::class,
// \illuminate\ssion\middleware\authenticatession::class,
\illuminate\view\middleware\shareerrorsfromssion::class,
\app\http\middleware\verifycsrftoken::class,
\illuminate\routing\middleware\substitutebindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
'cors'
],
];
/**
* the application's route middleware.
*
* the middleware may be assigned to groups or ud inpidually.
*
* @var array
*/
protected $routemiddleware = [
'auth' => \illuminate\auth\middleware\authenticate::class,
'auth.basic' => \illuminate\auth\middleware\authenticatewithbasicauth::class,
'bindings' => \illuminate\routing\middleware\substitutebindings::class,
'can' => \illuminate\auth\middleware\authorize::class,
'guest' => \app\http\middleware\redirectifa活络油uthenticated::class,
'throttle' => \illuminate\routing\middleware\throttlerequests::class,
'cors' => \app\http\middleware\accesscontrolalloworigin::class,
];
}
1
2
3
route::middleware('cors')->group(function () {
//
});
以上就是laravel api跨域访问的实现步骤的详细内容。
本文发布于:2023-04-07 19:28:46,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/7902d60af3d1d577a6ad5de067bfed5b.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Laravel API跨域访问的实现步骤.doc
本文 PDF 下载地址:Laravel API跨域访问的实现步骤.pdf
| 留言与评论(共有 0 条评论) |