如何从 PHP 文件中的 jQuery.getJSON 获取数据传输的数据?
我有一个Javascript变量whatToRefresh,它是这样定义的:
var whatToRefresh =
{
"online" : true,
"running" : false,
"detail" : 0
};
这个变量在getJSON中使用来告诉php文件请求什么数据:
$.getJSON("scripts/php/RequestData.php", whatToRefresh, function(data) { PopulateData(data); });
现在我需要PHP文件中的数据,但这总是返回null:
$requestData = json_decode(($_GET['data']), true);
我怎样才能在 php 中访问这些数据?
I have a Javascript variable whatToRefresh which is defined in this way:
var whatToRefresh =
{
"online" : true,
"running" : false,
"detail" : 0
};
This variable is used within a getJSON to tell the php file what data are requested:
$.getJSON("scripts/php/RequestData.php", whatToRefresh, function(data) { PopulateData(data); });
Now I need the data within the PHP file but this returns all the time null:
$requestData = json_decode(($_GET['data']), true);
How can I access this data within php?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需访问 $_GET['online']、$_GET['running']、$_GET['detail']。尝试看看 - var_dump($_GET);
just access $_GET['online'], $_GET['running'], $_GET['detail']. try to see that - var_dump($_GET);
您感到困惑的是
$.getJSON
方法。JSON 不会发送到服务器,因此您不需要使用
json_decode
对其进行解码。这个 jQuery 方法只是发送一个 HTTP
get
请求,以及一个包含变量的查询字符串。$.getJSON
方法期望看到来自服务器的 JSON 响应,因此名称中包含json
。Where you're getting confused is, the
$.getJSON
method.JSON is not being sent to the server, so you do not need to decode it with
json_decode
.This jQuery method is just sending an HTTP
get
request, with a query string that has your variables in it. The$.getJSON
method expects to see a JSON response from the server, hence thejson
in the name.