简体   繁体   English

使用 Axios 到 Laravel API 发出删除请求会引发错误,但在失眠时工作正常

[英]Making a delete request using Axios to Laravel API throws an error but works fine in insomnia

Hi I am creating a web app using create-react-app which consumes endpoints developed in laravel 5.嗨,我正在使用 create-react-app 创建一个 Web 应用程序,它使用在 laravel 5 中开发的端点。

When I make a Delete request using axios to the server, I get this error Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:8000/api/carts/7?api_token={api_token} .当我使用 axios 向服务器发出删除请求时,出现此错误跨域请求被阻止:同源策略不允许读取http://127.0.0.1:8000/api/carts/7?api_token= 上的远程资源{api_token} (Reason: Did not find method in CORS header 'Access-Control-Allow-Methods') Whereas, when I send the same request on Insomnia (HTTP Rest Client), I donot get any error. (原因:未在 CORS 标头“Access-Control-Allow-Methods”中找到方法)然而,当我在 Insomnia(HTTP Rest Client)上发送相同的请求时,我没有收到任何错误。

Here is the react code这是反应代码

handleDelete = ( cartId ) => {
    const api_token = localStorage.getItem('jbs_user_api_token');

    console.log("Delete cart item: ", cartId);

    axios.delete(`carts/${cartId}?api_token=${api_token}`)
        .then( res => {
             console.log(res);
        })
       .catch( err => {
             console.log(err);
        });
    }

Following is the endpoint in Laravel 5, the react app is making the aforementioned request to以下是 Laravel 5 中的端点,反应应用程序正在向

Route::resource('carts', 'API\CartAPIController');

Here is the method (laravel) that handles the delete request这是处理删除请求的方法(laravel)

public function destroy($id)
{
    $cart = $this->cartRepository->findWithoutFail($id);

    if (empty($cart)) {
        return $this->sendError('Cart not found');

    }

    $cart = $this->cartRepository->delete($id);

    return $this->sendResponse($cart, __('lang.deleted_successfully', ['operator' => __('lang.cart')]));

}

Definitely, there is no error on the API (Backend) since the request works fine in insomnia.当然,API(后端)上没有错误,因为请求在失眠时工作正常。 Plus both the applications (react and laravel) are served from localhost.此外,两个应用程序(react 和 laravel)都是从 localhost 提供的。

Please help请帮忙

In your package.json file of frontend/client folder try to add proxy like this在前端/客户端文件夹的 package.json 文件中尝试添加这样的代理

  "name": "app_name",
  "proxy": "http://127.0.0.1:8000"

The issue is resolved by adding Laravel Cors package in the laravel App (API Provider).通过在 Laravel 应用程序(API 提供程序)中添加 Laravel Cors 包解决了该问题。

Following is the link to Laravel Cors pakage以下是 Laravel Cors 包的链接

https://github.com/fruitcake/laravel-cors https://github.com/fruitcake/laravel-cors

暂无
暂无

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

相关问题 Axios GET 请求在 Postman 中工作但抛出 400 错误 - Redux - Axios GET Request works in Postman but throws 400 error - Redux 405 错误使 axios put/patch 请求从 vue 到 laravel - 405 error making axios put/patch request from vue to laravel Axios 如果第一个请求返回 400,则第二个请求将失败并出现 CORS 错误,但如果返回 200,则工作正常 - Axios second request fails with a CORS error if the first request returns a 400 but works fine if it returns a 200 对 api 的请求在组件中可以正常工作,但在将提供程序与 react 一起使用时却不行 - Request to api works fine in component but not when using provider with react Axios 发布请求在提交时抛出 431 错误 - Axios post request throws 431 error on submit axios.post给出了CORS问题,而GET在使用Laravel API的React中工作正常 - axios.post gives CORS issue whereas GET is working fine in React using Laravel API REST API 端点没有响应 axios 反应,但在浏览器中工作正常 - REST API endpoint returns no response with axios in react, but works fine in browser Axios HTTP DELETE请求返回415错误 - Axios HTTP DELETE request returns 415 error 使用axios在React中进行两个API调用 - Making Two API calls in react using axios Axios post fails with 403 error code and showing CSRF token validation failed but works fine django api when passing csrftoken in header with Postman - Axios post fails with 403 error code and showing CSRF token validation failed but works fine django api when passing csrftoken in header with Postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM