如何处理“使用get/head方法”的“请求”不能遇到身体问题。在vuejs?

发布于 2025-02-11 08:33:17 字数 920 浏览 3 评论 0原文

我在检查一些URL及其状态代码时有问题。我有一个PHP功能

public function checkUrl(Request $request)
{
    $curl = curl_init($request->url);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_exec($curl);
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    return $code;
}

,在VUE中,当我检查它们时,我将其称为

Object.keys(this.items).forEach(key => {
      let value = this.items[key];
      let checkUrl = value.url + this.usersearch;  
      fetch('/api/checkurl', {
            method: 'GET',
               headers: {
                   'Content-Type': 'application/json',                                                    
               },
            mode:'cors',
            cache:'default',
            body: JSON.stringify({url:checkUrl}),
     })

“ TypeError:无法构造“请求”:使用get/head方法的请求无法具有身体。 使用方法,帖子工作正常,但是我需要用GET方法检查URL。如果有人知道我在这里做错了什么,我会非常感激。

I have a problem with checking some URLs and their status code. I have a php function

public function checkUrl(Request $request)
{
    $curl = curl_init($request->url);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_exec($curl);
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    return $code;
}

And in Vue I call it like this

Object.keys(this.items).forEach(key => {
      let value = this.items[key];
      let checkUrl = value.url + this.usersearch;  
      fetch('/api/checkurl', {
            method: 'GET',
               headers: {
                   'Content-Type': 'application/json',                                                    
               },
            mode:'cors',
            cache:'default',
            body: JSON.stringify({url:checkUrl}),
     })

When I check them I get the Uncaught TypeError: Failed to construct 'Request': Request with GET/HEAD method cannot have body.
With method POST works fine, but I need to get the URLs checked with GET method. If anyone has idea what am I doing wrong here, I would be very grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2025-02-18 08:33:17

您是在GET请求正文中发送url参数,而HTTP协议不允许。获得请求没有身体。而是将其作为URL参数发送:

Object.keys(this.items).forEach(key => {
      let value = this.items[key];
      let checkUrl = value.url + this.usersearch;
      let query = new URLSearchParams({
        url: checkUrl
      });
      fetch('/api/checkurl?' + query, {
            method: 'GET',
            mode:'cors',
            cache:'default'
     });
});

另外,请确保为GET请求配置您的后端路由。

You are sending the url param in the body of a GET request, which is not allowed by the HTTP protocol. GET requests don’t have a body. Send it as an URL param instead:

Object.keys(this.items).forEach(key => {
      let value = this.items[key];
      let checkUrl = value.url + this.usersearch;
      let query = new URLSearchParams({
        url: checkUrl
      });
      fetch('/api/checkurl?' + query, {
            method: 'GET',
            mode:'cors',
            cache:'default'
     });
});

Also, make sure your backend route is configured for GET requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文