【问题标题】:HUE API JavaScript HTTP PUT Request returning "net::ERR_EMPTY_RESPONSE"HUE API JavaScript HTTP PUT 请求返回“net::ERR_EMPTY_RESPONSE”
【发布时间】:2016-03-23 22:35:30
【问题描述】:
  1. GET 请求工作正常。

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

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

    例子:

    选项http://xxx.xxx.x.x/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    -

感谢任何帮助。

【问题讨论】:

    标签: javascript angularjs ajax put philips-hue


    【解决方案1】:

    这对我有用:-

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

    【讨论】:

    • 感谢安吉拉的回复!可悲的是,我仍然遇到同样的错误。我猜我配置错误。你有什么特别的设置吗?我试过从 wamp 目录和常规目录运行它。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 2012-02-28
      • 1970-01-01
      相关资源
      最近更新 更多