当 AJAX-get 在本地主机上与 node.js 时收到错误消息
Getting error message on while AJAX-get on localhost with node.js
我正在尝试使用 AJAX 从使用 node.js
制作的本地服务器获取一段字符串,连接正在完成,状态为 200,一切正常,但我仍然无法获取该字符串。有人可以给我解释为什么会发生这种情况吗?
PS:我还是新手node.js
Node.js
服务器代码:
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/javascript"});
response.end("var a = 'Hello World';");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
AJAX
呼叫代码:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.ajax({
async: 'false',
type: 'GET',
url: 'http://127.0.0.1:8000/',
success: function(data, textStatus){
console.log('DONE: Request has been successfully sent');
},
error: function(xhr, textStatus, errorThrown){
console.log('ERR: Request failed 'nSTATUS: '+textStatus+''nERROR: '+errorThrown);
console.log(xhr);
}
});
</script>
安慰:
XMLHttpRequest cannot load http://127.0.0.1:8000/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
XHR failed loading: GET "http://127.0.0.1:8000/".send @ jquery-latest.min.js:4m.extend.ajax @ jquery-latest.min.js:4(anonymous function) @
ERR: Request failed
STATUS: error
ERROR:
Object {readyState: 0, responseText: "", status: 0, statusText: "error"}
Cross domain
经常因为same origin policy
violation
而使ajax调用失败。因此,您需要修改代码并删除crossDomain : true,
。此外,为了安全起见,请添加timeout:5000,
或写入async: false,
。
然后它会起作用。
相关文章:
- Node.js上的WebSocket,并在所有连接的客户端之间共享消息
- 如何在主干js中解析视图中的消息
- 在node.js应用程序中接收JMS消息
- 通过node.js和socket.io在两个用户之间发送消息
- 使用Modal UI覆盖JS Alert()消息
- 更改路线时,在react.js中得到一条非常crypric的消息
- js中的本地化字符串消息
- jQuery/JS Mimic Facebook's不在页面上时,标题在新消息上闪烁
- 帆上的自定义和多语言消息如何出错 js.
- JS脚本未按预期工作,没有错误消息
- 使用官方 Node.js SendGrid API 发送电子邮件时,响应 JSON 中可能出现哪些错误消息
- Parsley.js-将标签值附加到错误消息中
- 如何使用angular js在表单上显示服务器端验证错误消息
- 如何在提交时向Happy.js添加确认消息
- 使用php-js-msyql的实时更新消息系统
- Protobuf over zeromq/zwssock to JSMQ/ProtoBuf.js 消息未完全接收
- ext.js 消息框按钮对齐方式
- JS消息'不是函数'CoffeeScript中
- 检索页面加载Strophe js消息
- PHP中的JS消息返回到HTML页面