简体   繁体   English

为什么php json响应不会将数据数组发送回给ajax jquery请求?

[英]Why php json response does not send back the data array to ajax jquery request?

I have made an ajax request to the php server where I send a data from a form request and I would to get back the same data from the server just for testing requests and responses of the same data throwing between the client and the server. 我向php服务器发出了一个ajax请求,在该服务器中我从表单请求发送了数据,我将从服务器中取回相同的数据,仅用于测试客户端和服务器之间抛出的相同数据的请求和响应。

$.ajax({
    method: "POST",
    url: "addRaces",
    dataType: "json",
    data : sending_data,
    success : ...
})

sending_data is an array like {field_form_name : value} sending_data是一个类似{field_form_name : value}的数组

The server responds to the request with the following code : 服务器使用以下代码响应请求:

$data = [];
foreach ($request->Input() as $key => $value) {
    $data[$key] = $value;
}

return json_encode($data);

The same data should come back to the client, but nothing happen with an alert(response) or a jquery showing debug data like $('#debug').html(response); 相同的数据应该返回给客户端,但是alert(response)或显示调试数据(例如$('#debug').html(response);

The strange thing coming up when I try to push the server answer like 当我尝试推送服务器答案时出现了奇怪的事情

return json_encode([
  'debug' => $data['name']
]);

and showing results to client like 并向客户显示结果

$('#debug').html(response.debug); $('#debug')。html(response.debug);

it is working for a single key-value response showing the properly value field I have send from the client. 它适用于显示我已从客户端发送的正确值字段的单个键值响应。 Other wise whether I send the full data as an array nothing is shown in the debug area of the client. 否则,是否将完整data作为数组发送给客户端,调试区域中什么也没有显示。

return json_encode($data); wont work. 不会工作。 as it is returning the json string to the calling method which is not your jQuery at front end. 因为它将json字符串返回给调用方法,而该调用方法不是前端的jQuery。 Don't confuse front end (html javascript, jQuery, etc...) with back end (PHP) the two of them are running in different machines. 不要将前端(html javascript,jQuery等)与后端(PHP)混淆,它们两个在不同的计算机上运行。 One at client end and another at your server. 一个在客户端,另一个在您的服务器。

you should use echo json_encode($data); 您应该使用echo json_encode($data); this will output the json string and your web server will send it to the front end. 这将输出json字符串,并且您的Web服务器会将其发送到前端。

Make sure you are echoing the data not returning as @bansi said, if you are echoing it already then debug using var_dump(json_encode($data)) . 确保您正在回显未返回的数据,如@bansi所述,如果您已经回显了数据,请使用var_dump(json_encode($data))进行调试。 If json_encode() found errors it will return False instead of the expected json array. 如果json_encode()发现错误,它将返回False而不是预期的json数组。

You should debug using the developer tools instead of using code $('#debug').html(response.debug); 您应该使用开发人员工具进行调试,而不要使用代码$('#debug').html(response.debug); . The Network tab of developer tools is very useful to debug HTTP requests, it will show response header and body of your requests. 开发人员工具的“网络”选项卡对于调试HTTP请求非常有用,它将显示响应标头和请求主体。

In your case, just change return to echo like @bansi's answer . 在你的情况,只是改变returnecho@半丝的答案 The return command does not print the data so you did not get anything in the response. return命令不会打印数据,因此您在响应中什么也没得到。

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

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