XMLHttpRequest - 来自 javascript 的 REST
XMLHttpRequest - REST from javascript
我的本地主机上有一个网络服务器 运行。如果我从我的网络服务器加载我的网页,一切正常。我可以用我的网络服务器打开一个 REST 会话。
JS代码:--
$(document).ready(function() {
var xhr = new XMLHttpRequest();
var open_str = "http://localhost:8080/vscp/rest?user=admin&password=d50c3180375c27927c22e42a379c3f67&format=json&op=1";
xhr.open("GET", open_str, true);
xhr.onreadystatechange = function() {
alert(xhr.readyState + "" + xhr.status);
if (xhr.readyState == 4 && xhr.status == 200) {
alert("session opend success");
var json = JSON.parse(xhr.responseText);
alert(JSON.stringify(json, null, 10));
}
}
xhr.send();
});
HTML代码:--
<!DOCTYPE html>
<html>
<head>
<title>Hello jQuery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="hello.js"></script>
</head>
<body>
<div>
<p class="greeting-id">Trying to open the REST session with vscpd </p>
</div>
</body>
</html>
现在,如果我从我的 D: 驱动器加载相同的 html 页面 :--
file:///D:my_folder/htm_test.html
我收到以下错误 "No 'Access-Control-Allow-Origin' header is present"。我已经检查了 javascript 代码,其中 xhr.readyState 是 4,xhr.status 是 0。
请建议对我的 javascript 代码进行哪些更改,这样,如果我使用 file:///
直接从我的 D: 驱动器打开 html 文件,那么也会打开 REST 会话与我的网络服务器正确连接。
========================= JSONP代码================== ======
$(document).ready(function() {
var url = "http://localhost:8080/vscp/rest?user=admin&password=d50c3180375c27927c22e42a379c3f67&format=json&op=1";
function jsonpCallback(response) {
alert('success');
}
$.ajax({
url: url,
dataType: 'jsonp',
error: function(xhr, status, error) {
alert("error" + " " + error.message);
},
success: jsonpCallback
});
return false;
});
我得到的错误:--
服务器正在发送正确的响应:--
{"success":true,"code":1,"message":"success","description":"Success","session-id":"e5a36e14b687c37b615dbc6a9506df5c","nEvents":0}
但是 ajax 调用给出了这个响应的错误,即 "Uncaught SyntaxError: Unexpected token :"
这是因为 Chrome 和其他一些浏览器出于安全原因阻止了本地文件,我认为没有解决此问题的方法。您必须使用网络服务器。
您已 运行 进入 Same Origin Policy - 这是一种安全机制,可限制从一个域加载的 JavaScript 向另一个域发送请求。
有 various ways around it, if you are using Google Chrome I would suggest setting the --allow-file-access-from-files flag when you start the browser, Firefox also provides a way to work around it,但不要忘记在完成测试后禁用这些选项,它们的存在是有充分理由的!
我的本地主机上有一个网络服务器 运行。如果我从我的网络服务器加载我的网页,一切正常。我可以用我的网络服务器打开一个 REST 会话。
JS代码:--
$(document).ready(function() {
var xhr = new XMLHttpRequest();
var open_str = "http://localhost:8080/vscp/rest?user=admin&password=d50c3180375c27927c22e42a379c3f67&format=json&op=1";
xhr.open("GET", open_str, true);
xhr.onreadystatechange = function() {
alert(xhr.readyState + "" + xhr.status);
if (xhr.readyState == 4 && xhr.status == 200) {
alert("session opend success");
var json = JSON.parse(xhr.responseText);
alert(JSON.stringify(json, null, 10));
}
}
xhr.send();
});
HTML代码:--
<!DOCTYPE html>
<html>
<head>
<title>Hello jQuery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="hello.js"></script>
</head>
<body>
<div>
<p class="greeting-id">Trying to open the REST session with vscpd </p>
</div>
</body>
</html>
现在,如果我从我的 D: 驱动器加载相同的 html 页面 :--
file:///D:my_folder/htm_test.html
我收到以下错误 "No 'Access-Control-Allow-Origin' header is present"。我已经检查了 javascript 代码,其中 xhr.readyState 是 4,xhr.status 是 0。
请建议对我的 javascript 代码进行哪些更改,这样,如果我使用 file:///
直接从我的 D: 驱动器打开 html 文件,那么也会打开 REST 会话与我的网络服务器正确连接。
========================= JSONP代码================== ======
$(document).ready(function() {
var url = "http://localhost:8080/vscp/rest?user=admin&password=d50c3180375c27927c22e42a379c3f67&format=json&op=1";
function jsonpCallback(response) {
alert('success');
}
$.ajax({
url: url,
dataType: 'jsonp',
error: function(xhr, status, error) {
alert("error" + " " + error.message);
},
success: jsonpCallback
});
return false;
});
我得到的错误:-- 服务器正在发送正确的响应:-- {"success":true,"code":1,"message":"success","description":"Success","session-id":"e5a36e14b687c37b615dbc6a9506df5c","nEvents":0}
但是 ajax 调用给出了这个响应的错误,即 "Uncaught SyntaxError: Unexpected token :"
这是因为 Chrome 和其他一些浏览器出于安全原因阻止了本地文件,我认为没有解决此问题的方法。您必须使用网络服务器。
您已 运行 进入 Same Origin Policy - 这是一种安全机制,可限制从一个域加载的 JavaScript 向另一个域发送请求。
有 various ways around it, if you are using Google Chrome I would suggest setting the --allow-file-access-from-files flag when you start the browser, Firefox also provides a way to work around it,但不要忘记在完成测试后禁用这些选项,它们的存在是有充分理由的!