无法从 Javascript 访问 Nest

failed to access Nest from Javascript

我正在尝试使用 java 脚本 实现对我的 Nest 帐户的访问(以同样的方式完成 before/and 它正在工作) android native app (java),我们成功访问nest账号如下:

final String BASE_URL =
        "https://home.nest.com/user/login?";
final String USERNAME_PARAM = "username";
final String PASSWORD_PARAM = "password";

Uri builtUri = Uri.parse(BASE_URL).buildUpon()
        .appendQueryParameter(USERNAME_PARAM, mEmail)
        .appendQueryParameter(PASSWORD_PARAM, mPassword)
        .build();

URL url = new URL(builtUri.toString());

// Create the request, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("http.agent", "Nest/1.1.0.10 CFNetwork/548.0.4");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.connect();

// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();

但是当我尝试在 Javascript 中执行相同操作时,出现以下错误:

XMLHttpRequest cannot load https://home.nest.com/user/login?username=myemail@domain.com&password=XXXX. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'domain.com' is therefore not allowed access. The response had HTTP status code 401.

我的Javascript:

var BASE_URL = "https://home.nest.com/user/login?";
var USERNAME_PARAM = "username";
var PASSWORD_PARAM = "password";

var url = BASE_URL +  USERNAME_PARAM + "=" + mEmail
    + "&" + PASSWORD_PARAM + "=" + mPassword; // pass in a URI as a string and parse that 


$.ajax({
    headers: { "http.agent": "Nest/1.1.0.10 CFNetwork/548.0.4" },
    type: "POST",
    url: url,
    contentType: "application/x-www-form-urlencoded",
    dataType: "json",
    success: function (response) {
        console.log("Success:" + JSON.stringify(response));           
    },
    error: function (response) {
        console.error("Error:" + JSON.stringify(response));
    }
});

知道问题是什么吗?,为什么它在 java 中有效,我也尝试使用 Postman - REST 客户端扩展,它运行完美!!!

谢谢, 约瑟夫

CORS。查看您问题的黄色部分...您的请求未通过跨站点脚本检查,这意味着您无法使用 AJAX 执行此调用,您可以使用 JSONP 吗?顺便说一句,JSONP 只能通过 GET 使用。

如果需要,解决方案是实施代理。