简体   繁体   English

如何从PHP文件中的jQuery.getJSON获取数据传输的数据?

[英]How can I get the data transfered data from jQuery.getJSON within the PHP file?

I have a Javascript variable whatToRefresh which is defined in this way: 我有一个Javascript变量whatToRefresh,它是以这种方式定义的:

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中使用此变量来告诉php文件请求什么数据:

$.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: 现在,我需要PHP文件中的数据,但这始终返回null:

$requestData = json_decode(($_GET['data']), true);

How can I access this data within php? 如何在php中访问此数据?

just access $_GET['online'], $_GET['running'], $_GET['detail']. 只需访问$ _GET ['online'],$ _ GET ['running'],$ _ GET ['detail']即可。 try to see that - var_dump($_GET); 尝试看看-var_dump($ _ GET);

Where you're getting confused is, the $.getJSON method. 让您感到困惑的是$.getJSON方法。

JSON is not being sent to the server, so you do not need to decode it with json_decode . JSON 发送到服务器,因此您不需要使用json_decode对其进行解码。

This jQuery method is just sending an HTTP get request, with a query string that has your variables in it. 这个jQuery方法只是发送一个HTTP get请求,其中包含一个包含您的变量的查询字符串。 The $.getJSON method expects to see a JSON response from the server, hence the json in the name. $.getJSON方法期望从服务器看到JSON 响应 ,因此名称中为json

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

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