【问题标题】:Axios post response is htmlaxios post响应是html
【发布时间】:2020-01-15 08:46:41
【问题描述】:

大家早上好,

我使用:“laravel/framework”:“^6.2”, "axios": "^0.19", "vue": "^2.5.17",

当我使用 axios.put 和 route put 我的 response.data 是正确的(response.data:“ok”)时我遇到了问题,但是在我的 axios.post 和 route post 中,我在 response.data 中收到了一个 html我不明白为什么有人可以解释一下?以及如何在我的 axios.post 中获得一个 reponse.data:"ok"?

文件.vue

let formData = new FormData();
                formData.append('connexion', this.customer);
                formData.append('product', this.product);

                axios.post('hosting/', formData)
                    .then(response=> {
                        console.log(response);
                    });

文件路径

Route::post('hosting', ['uses' => 'Api\HostingController@cart']);

文件控制器

public function cart(Request $request)
{
    return 'ok';
}

我在 axios.post 时的回应

我在 axios.put 时的回应

感谢您的帮助!

【问题讨论】:

  • 可以显示整个 HTML 吗?
  • 他只是归还了我的主刀。
  • 您是否尝试过在请求头字段中添加 'Accept': 'application/json' ?在返回数据时使用return response()->json(/* your data here */)?

标签: laravel vue.js vuejs2 axios


【解决方案1】:

[更新] 我终于解决了这个问题,只需删除我的 url POST 上的“/”

POST 向我发送 html 消息的原因是,在 POST 中,如果“/”它等待某些参数,然后在 PUT 中它会自动删除“/”,因为后面没有参数!

axios.post('hosting', formData)
      .then(response=> {
              console.log(response);
            });

真的是一个愚蠢的错误对不起。我把答案放在帮助其他人的地方。

【讨论】:

    【解决方案2】:

    我以前遇到过这个问题。我的路线不是我必须删除斜线的路线。 这是一个实际的 API URL(例如 https://myapiurl.com/v1/upload),我正在向其发出 POST 请求。我的请求成功,但我得到了 HTML 作为响应数据。

    原因: 我将请求 URL 作为环境变量并引用它。 所以我在做类似的事情

    const URL = process.env.MY_URL || "";
    

    由于某种原因,Axios 向 URL="" 而非 URL 发出请求,这就是我获取 HTML 作为响应数据的原因。

    所以这个问题的启示是,检查你的请求 URL。

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 2020-10-26
      • 1970-01-01
      • 2021-04-30
      • 2020-10-09
      • 1970-01-01
      • 2021-04-18
      • 2018-10-11
      • 1970-01-01
      相关资源
      最近更新 更多