[英]Can't get JSON response on Laravel 6
因此,我使用 ajax 请求从 php 端点获取数据,作为基础,我使用了我在登录表单上使用的代码,该代码也返回数据并且该代码正在工作,因此我对此感到有些困惑。
这是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:
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' ]);
}
}
由于没有数据发回,我不断收到“未定义” 。
奇怪的是,正如我所提到的,我有一个非常相似的代码在表单上工作,我确定这是我遗漏的东西,但我无法弄清楚。
编辑:来自问题的额外信息
目前正在SUCCESS下测试,行: console.log( response['data']);
还尝试了console.log(response)和console.log(response.data)并且也未定义。
URI 被排除在 csrf 保护之外
dataType: "json" 已因之前的问题而被删除,并且一个非常相似的代码已经在为另一个功能工作了。
在网络下,我得到“此请求没有可用的响应数据” ,考虑到“未定义”变量,这是有道理的
error_reporting 已经是 E_ALL 并且我没有收到任何错误。
使用简化版本的更新代码进行测试,但仍然无法正常工作。
好的,所以我想通了,没有什么比枕边谈话和休息更像的了,所以我正在调用另一个方法 (getProductVariations) 并在那里返回信息,但是被调用的方法 (getData) 没有返回任何信息。
于是捕获了第二个方法返回的数据,用初始方法返回就完成了。
$response = $this->getProductVariations();
return $response;
谢谢大家的时间和建议
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.