简体   繁体   English

CORS / JavaScript / Laravel:向Digital Ocean服务器发送请求并接收数据(标头问题)

[英]CORS/JavaScript/Laravel: Sending request to Digital Ocean server and receiving data (Headers issues)

So, what I'm trying to do is use a Digital Ocean droplet as an api for an application hosted on a different server. 因此,我想做的是使用Digital Ocean Droplet作为托管在不同服务器上的应用程序的api。 Currently, I'm just developing so this server is from my localhost:3000. 目前,我正在开发中,因此该服务器来自本地主机:3000。

On my client side code (JavaScript) I have: 在我的客户端代码(JavaScript)上,我有:

  handleSendData = () => {
    const request = new XMLHttpRequest()
    request.open('POST', 'http://my-droplet-ip/api/create-image')
    request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8')
    request.setRequestHeader('Access-Control-Allow-Origin', 'http://my-droplet-ip')
    request.send(JSON.stringify(data-object))
  }

Finally, in my Laravel application (Laravel Framework 5.8.18) I have a route under routes/api.php : 最后,在我的Laravel应用程序(Laravel Framework 5.8.18)中,我在routes/api.php下有一条路由:

Route::post('/create-image', 'CreateData');

And I have a controller CreateData.php : 而且我有一个控制器CreateData.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CreateImage extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        return response('Hello World', 200)
            ->header('Content-Type', 'application/json; charset=UTF-8')
            ->header('Access-Control-Allow-Origin', '*');
    }
}

The problem is when I try to run this request, I get a CORS error: 问题是当我尝试运行此请求时,出现CORS错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://my-droplet-ip/api/create-image . 跨域请求被阻止:同源策略禁止读取位于http:// my-droplet-ip / api / create-image的远程资源。 (Reason: CORS header 'Access-Control-Allow-Origin' missing) (原因:CORS标头“ Access-Control-Allow-Origin”缺失)

Under the networking tab I get a 404 not found and the Access header is not there in the response. 在[网络]标签下,我找不到404,并且响应中没有Access标头。

Any thoughts out there on this issue? 关于这个问题有什么想法吗?

There is a simple solution to this and it is ok for testing on you local machine. 有一个简单的解决方案,可以在本地计算机上进行测试。

You could just put in index file to allow cors, but I suggest you building middleware for this. 您可以只放入索引文件以允许使用cors,但我建议您为此构建中间件。

Here is a link it is explained really nice:) 这是一个链接,解释得非常好:)

https://medium.com/@petehouston/allow-cors-in-laravel-2b574c51d0c1 https://medium.com/@petehouston/allow-cors-in-laravel-2b574c51d0c1

You can put also this in index.php 您也可以将其放在index.php中

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

Hope it helps :D 希望对您有帮助:D

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

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