我需要将带有有效负载的PUT请求发送到API(Philips Hue Bridge),但是我在javascript中有一些限制。
我可以仅使用:
open(method, url [, async = true [, username = null [, password = null]]])
设置请求方法,请求URL和同步标志。
支持的请求方法:GET,POST
send(data = null)
发起请求。可选的“ data”参数仅允许使用UTF-8编码的字符串类型。如果请求方法是GET,则忽略该参数。
abort()
取消任何网络活动。
此限制来自Tizen Wearable Web Widget Specification
URL: http://hostname/api/username/lights/lightKey/state
方法: PUT
有效负载示例: {"on": true}
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://hostname/api/username/lights/lightLey/state?on=true');
xhr.send();
响应: 400 (Bad request)
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://hostname/api/username/lights/lightLey/state?on=true');
xhr.send();
响应: 400 (Bad request)
还尝试了GET
和POST
使用以下URL结尾:
../state?{on=true}
../state?{on:true}
../state?{"on"=true}
../state?{"on":true}
响应: 400 (Bad request)
同样,除了上面提到的XMLHttpRequest方法之外,我不能再使用任何其他库或命令。
我该如何解决?
答案 0 :(得分:1)
是示例代码。您可以在代码中尝试这种方式。
var http = new XMLHttpRequest();
var url = 'your_URL.php';
var params = 'on=true';
http.open('PUT', url, true);
//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
答案 1 :(得分:0)
将method
属性更改为PUT
应该可以解决问题。
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'http://hostname/api/username/lights/lightLey/state?on=true');
xhr.send();