[英]use getJSON with variable
如果問題不清楚,我會事先道歉,但我會盡力的。
目前,我有這段代碼可以從我的家庭自動化控制器中提取所有設備的狀態。
function pull_light_status (lights_array) {
$.getJSON( "resources/php/getjson.php", function( json ) {
var vera_obj = json;
$.each(lights_array,function(myindex, myvalue){
var id_del_object = myvalue;
var id_status = vera_obj.devices[id_del_object].states[0].value;
})
})
}
由於對象的結構,我很難使用該對象,因此我在考慮使用另一條路線。
我可以使用此名為change_state.php的php獲取特定設備的狀態
<?php
$url = 'http://ip:port/data_request?id=variableget&DeviceNum' . $_POST['device_id'] . "&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status";
$jsondata_send = file_get_contents($url);
//echo $jsondata_send;
?>
我的問題是:
是否可以將JS替換為允許我請求我指定的設備變量的json響應的內容?
像這樣嗎
function pull_light_status (lights_array) {
$.each(lights_array,function(myindex, myvalue){
$.getJSON( "resources/php/change_state.php",{ device_id: + myvalue})
.done(function(json) {
var vera_obj = json;
console.log (vera_obj);
})
})
}
我期望得到的結果是0或1,這樣我就可以了解設備的狀態了。
嘗試使用$.post
因為您期望php
端具有POST
負載。
function pull_light_status (lights_array) {
$.each(lights_array,function(myindex, myvalue){
$.post( "resources/php/change_state.php", { device_id: myvalue })
.done(function( data ) {
var vera_obj = json;
console.log (vera_obj);
});
})
}
在php
端,您需要確保要發送回json
編碼的字符串。
所以是var_dump($jsondata_send);
可以的話,如果可以的話,將其與標題一起打印出來。
<?php
$url = 'http://ip:port/data_request?id=variableget&DeviceNum' . $_POST['device_id'] . "&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status";
$jsondata_send = file_get_contents($url);
header('Content-Type: application/json');
print $jsondata_send;
?>
如果要改用getJSON
,請在php文件中將$_POST['device_id']
更改為$_GET['device_id']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.