【发布时间】:2020-06-10 16:37:41
【问题描述】:
所以我使用 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,我没有收到任何错误。
使用简化版本进行测试的更新代码,仍然无法正常工作。
【问题讨论】:
-
console.log(response)看看它包含什么。 -
调用了哪个方法?
success:还是error:?任何一个都可以在您的日志中返回undefined。您是否在请求中包含_token?或者排除该路由上的 CSRF 保护? -
Currently testing under SUCCESS the line: console.log( response['data']); console.log 还尝试了 console.log(response) 并且也未定义
-
与其猜测,不如尝试在浏览器开发工具的网络选项卡中查看请求,看看实际返回了什么
-
如果没有响应数据,PHP 可能会在您的响应行之前抛出错误。尝试打开错误报告
error_reporting(E_ALL);来查看。您的问题似乎出在服务器端,因此分享更多 PHP 代码可能有助于解决此问题。