0
  1. GET 请求工作正常。

  2. 来自 API 调试工具的 PUT 请求工作正常。

  3. PUT 和 POST,无论使用的是数据还是 API url,都会返回以下错误:

    例子:

    选项http://xxx.xxx.xx/api/AuthorizedUsername/APIRequest/lights/light/1/state net::ERR_EMPTY_RESPONSE

它返回一个空响应。

我相关的 Angular JS 代码:

var bridgeIp = "http://xxx.xxx.x.x";
var username = "authenticatedUsername";
var lightData = {"on": true};

$http.put(bridgeIp+"/api/"+username+"/lights/5/state", lightData)
.then(function success(response) {
          console.log(response.data);
     }, function error(e){
          console.log(e);
     });

我也尝试过使用 JQuery 的 Ajax 函数并收到同样的问题。

我使用 fiddler 比较了请求,但这导致了 CORS 错误。所以我然后将这些请求与查尔斯进行了比较。以下是这些结果:

从 API 调试工具:

URL http://xxx.xxx.x.x/api/AuthorizedUser/lights/5/state
Status  Complete
Response Code   200 OK
Protocol    HTTP/1.1
SSL -
Method  PUT
Kept Alive  No
Content-Type    application/json
Client Address  /127.0.0.1
Remote Address  xxx.xxx.x.x/xxx.xxx.x.x
Timing  
Request Start Time  3/23/16 18:24:17
Request End Time    3/23/16 18:24:17
Response Start Time 3/23/16 18:24:17
Response End Time   3/23/16 18:24:17
Duration    43 ms
DNS 0 ms
Connect 2 ms
SSL Handshake   -
Request 2 ms
Response    1 ms
Latency 37 ms
Speed   20.46 KB/s
Response Speed  879.88 KB/s
Size    
Request Header  427 bytes
Response Header 421 bytes
Request 12 bytes
Response    41 bytes
Total   901 bytes
Request Compression -
Response Compression    -

从我通过 JavaScript 的 http 请求:

URL http://xxx.xxx.x.x/api/AuthorizedUser/lights/5/state
Status  Failed
Failure Remote server closed the connection before sending response header
Response Code   -
Protocol    HTTP/1.1
SSL -
Method  OPTIONS
Kept Alive  No
Content-Type    -
Client Address  /127.0.0.1
Remote Address  xxx.xxx.x.x/xxx.xxx.x.x
Timing  
Request Start Time  3/23/16 18:24:55
Request End Time    3/23/16 18:24:55
Response Start Time -
Response End Time   3/23/16 18:24:55
Duration    14 ms
DNS 0 ms
Connect 2 ms
SSL Handshake   -
Request 2 ms
Response    -
Latency -
Speed   31.74 KB/s
Response Speed  -
Size    
Request Header  455 bytes
Response Header -
Request -
Response    -
Total   455 bytes
Request Compression -
Response Compression    -

任何帮助表示赞赏。

4

2 回答 2

0

这对我有用:-

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("PUT", 'http://<local address>/api/<username>/groups/1/action');
xmlhttp.send('{"on":true, "hue": 16000, "bri": 254'}');

于 2016-04-11T10:16:49.697 回答
0

您的 JavaScript 输出将 Method 显示为 OPTIONS。如果那是帖子顶部列出的选项,那么看起来是错误的,因为它说...APIRequest/ lights/light /1/state,但可能应该是...APIRequest/ lights /1/state

于 2016-04-26T02:35:27.580 回答