简体   繁体   English

无法在 Laravel 6 上获得 JSON 响应

[英]Can't get JSON response on Laravel 6

So I'm using an ajax request to get data from a php endpoint, as base I used a code I'm using on a login form which also returns data and that one is working so I'm a bit puzzled by this.因此,我使用 ajax 请求从 php 端点获取数据,作为基础,我使用了我在登录表单上使用的代码,该代码也返回数据并且该代码正在工作,因此我对此感到有些困惑。

This is the JS:这是JS:

jQuery.ajax({
    type: "POST",
    url: url,
    data: {
        ws: ws,
        productor: productor,
        product: product
    },
    success: function (response) {
        console.log( response ); // <-- sayd "undefined" on console
    },
    error: function (response) {
        console.error( lang["WS.error"] );
    }
});

Laravel PHP: Laravel PHP:

class PMWSjs extends Controller
{
    private $PMWShandler;
    private $parameters;

    public function getData (Request $request)
    {
        $this->PMWShandler = new PMWShandler();

        // Gets sent variables variables
        $this->parameters = $request->all();

        switch ($this->parameters["ws"]) {
            case "getProductVariations":
                $this->getProductVariations();
                break;

            default:
                return false;
        }
    }

    public function getProductVariations()
    {
        // we get here but nothing is returned, maybe issue is related to response() ? 
        return response()->json([ 'success' => true , 'data' => 'test' ]);
    }

}

I keep getting "undefined" since no data is sent back.由于没有数据发回,我不断收到“未定义”

The weird thing is as I mentioned I have a very similar code working on a form, I'm sure it's something I'm missing but I can't figure it out.奇怪的是,正如我所提到的,我有一个非常相似的代码在表单上工作,我确定这是我遗漏的东西,但我无法弄清楚。

EDIT: extra info from questions编辑:来自问题的额外信息

Currently testing under SUCCESS , line: console.log( response['data']);目前正在SUCCESS下测试,行: console.log( response['data']);

Also tried console.log(response) and console.log(response.data) and get undefined as well.还尝试了console.log(response)console.log(response.data)并且也未定义。

URI is excluded from csrf protection URI 被排除在 csrf 保护之外

dataType: "json" was removed for previous issues and again a very similar code is working for another feature already. dataType: "json" 已因之前的问题而被删除,并且一个非常相似的代码已经在为另一个功能工作了。

Under network I get "this request has no response data available" which makes sense considering the "undefined" variables在网络下,我得到“此请求没有可用的响应数据” ,考虑到“未定义”变量,这是有道理的

error_reporting is already E_ALL and I'm getting no errors. error_reporting 已经是 E_ALL 并且我没有收到任何错误。

UPDATED CODE WITH A SIMPLIFIED VERSION FOR TESTING, STILL NOT WORKING.使用简化版本的更新代码进行测试,但仍然无法正常工作。

Ok everybody so I figured it out, there is nothing like pillow talking and rest, so I'm calling another method (getProductVariations) and returning the info there, but the method that was called (getData) was returning nothing.好的,所以我想通了,没有什么比枕边谈话和休息更像的了,所以我正在调用另一个方法 (getProductVariations) 并在那里返回信息,但是被调用的方法 (getData) 没有返回任何信息。

So captured the second method returned data and return it with the initial method and done.于是捕获了第二个方法返回的数据,用初始方法返回就完成了。

$response = $this->getProductVariations();
return $response;

Thank you all for your time and suggestions谢谢大家的时间和建议

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

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