代码之家  ›  专栏  ›  技术社区  ›  Learner

Axios响应数据始终设置为Larvel中的空字符串

  •  4
  • Learner  · 技术社区  · 6 年前

    我从来没有从后端返回过奇怪的问题。。

    return response()->json(['message' => 'Success!']);

    我也试过:

    return response(['message' => 'Success!']);
    return ['message' => 'Success!'];
    

    当我 console.log() 回应:

    axios.post(url).then((response)=>{
        console.log(response);
    });
    

    我得到了所有关于响应和数据的空字符串,我在Laravel5.7中遇到了这个问题,在5.6中工作得很好。。。

    0 回复  |  直到 6 年前
        1
  •  1
  •   jpm    5 年前

    在你的刀片,确保你有 csrf_token 在头上

    xyz.blade.php

    <head>
        <meta name="csrf-token" content="{{ csrf_token() }}">
    </head
    

    确保你的方法是正确的获取,发布,删除,放入路由文件( web.php 或任何东西)

    axios.post('/some/url', {
               post_param_a : 1, 
                post_param_b : 2
             }).then(response => {
                  //your value should be in response.data object
                  console.log(response.data.message)
                })
                .catch(function (error) {
                    console.log(error);
                });
    

    public function methodName(){
        return response()->json(['message' => 'Success!']);
    }
    

    右键单击浏览器>检查>网络选项卡>参考屏幕截图。

    enter image description here

    你也可以 dd($response) 您在“inspector in response”部分中的响应和视图

    推荐文章