是否可以使用ajax/javascript原型解析远程文件
我想解决这个难题,但该文件驻留在远程服务器上。我如何解析这个文件,因为我不断收到此错误。
XMLHttpRequest 无法加载 http://www.weebly.com/weebly/publicBackend.php。 Access-Control-Allow-Origin 不允许来源 http://mysite.com。 拒绝获取不安全标头“X-JSON”
代码如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript" src="prototype.js"></script>
<!-- Puzzle starts here -->
<script type='text/javascript'>
// Note: there may be some unfinished code here, that needs finishing...
// You should probably try to get this function working...
function solvePuzzle() {
new Ajax.Request('http://www.weebly.com/weebly/publicBackend.php', {
parameters:{
pos: 'solvepuzzle'
},
onSuccess:handlerSolvePuzzle,
onFailure:function() { alert('Transmission error. Please try again.'); }
});
}
function handlerSolvePuzzle(t) {
var responseText = t.responseText;
responseText = responseText.replace(/\n/, "");
if (responseText.match(/!!$/)) {
alert("Oops: "+responseText);
} else {
// Still need to decode the response
// Once the response is decoded, we can fire off the alert
// giving the user further instructions
//alert(responseText);
//alert('To complete the challenge, '+t.responseText);
}
}
</script>
</head>
<body>
<input type="button" onclick="solvePuzzle()" value="hello"/>
</body>
</html>
I want to solve this puzzle but the file resides on remote server. How can I parse this file because I keep getting this error.
XMLHttpRequest cannot load http://www.weebly.com/weebly/publicBackend.php. Origin http://mysite.com is not allowed by Access-Control-Allow-Origin.
Refused to get unsafe header "X-JSON"
Code Below
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript" src="prototype.js"></script>
<!-- Puzzle starts here -->
<script type='text/javascript'>
// Note: there may be some unfinished code here, that needs finishing...
// You should probably try to get this function working...
function solvePuzzle() {
new Ajax.Request('http://www.weebly.com/weebly/publicBackend.php', {
parameters:{
pos: 'solvepuzzle'
},
onSuccess:handlerSolvePuzzle,
onFailure:function() { alert('Transmission error. Please try again.'); }
});
}
function handlerSolvePuzzle(t) {
var responseText = t.responseText;
responseText = responseText.replace(/\n/, "");
if (responseText.match(/!!$/)) {
alert("Oops: "+responseText);
} else {
// Still need to decode the response
// Once the response is decoded, we can fire off the alert
// giving the user further instructions
//alert(responseText);
//alert('To complete the challenge, '+t.responseText);
}
}
</script>
</head>
<body>
<input type="button" onclick="solvePuzzle()" value="hello"/>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Chrome 和 Firefox 的开发者工具允许您动态修改 JS。
如果您使用的是 Chrome,请转到菜单“视图”->“开发人员”->“JavaScript 控制台”,打开控制台。从页面源复制js。改变它。然后将更改后的 JavaScript 函数粘贴到控制台中。按回车键。然后开始输入“solvePuzzle();”按回车键。您会看到响应返回。
对于 Firefox,您需要下载 Firebug 插件。
Chrome and Firefox's developer tools allow you to modify JS on the fly.
If you're on Chrome, open up the console by going to the menu View->Developer->JavaScript Console. Copy the js from the page source. Alter it. Then paste altered javascript function(s) into the console. Hit enter. Then start typing 'solvePuzzle();' Hit enter. You'll see the response come back.
For Firefox, you'll need to download Firebug plugin.
由于同源政策,您无法从 JavaScript 执行此操作: https://developer.mozilla.org/en/ Same_origin_policy_for_JavaScript。如果此 weebly 站点支持某种 JSON API,您可以使用 JSONP: http://en.wikipedia.org /wiki/JSONP。除此之外,由于客户端的安全限制,您可能最好通过服务器端与此站点进行交互。
You cannot do this from JavaScript due to the same origin policy: https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript. If this weebly site supports some sort of JSON API, you could use JSONP: http://en.wikipedia.org/wiki/JSONP. Other than that, you're probably better off interacting with this site via the server side due to security restrictions on the client side.
考虑在“mysite.com”上安装 HTTP 隧道,以便浏览器不必直接访问“weebly.com”。
Consider installing a HTTP tunnel on your "mysite.com" so that the browser does not have to access "weebly.com" directly.