在一个项目上想用NodeJS,在前端的JS(http://localhost/xxx)中ajax访问后端RestAPI(http://localhost:3000/….)时(Chrome)报错:

XMLHttpRequest cannot load http://localhost:3000/auth/xxx/xxx. Originhttp://localhost is not allowed by Access-Control-Allow-Origin.

本地测试解决方案:

  1. 使用Chrome插件:Allow-Control-Allow-Origin: *
  2. 为Chrome浏览器添加启动属性: --disable-web-security

jQuery解决方案:

使用 Ajax Cross Origin - jQuery plugin ajax跨域请求

NodeJS解决方案:

  • 方案一:Express
var express = require('express');
var app = express();
//设置跨域访问
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
}); app.get('/auth/:id/:password', function(req, res) {
res.send({id:req.params.id, name: req.params.password});
}); app.listen(3000);
console.log('Listening on port 3000...');
var express = require('express');
var app = express(); app.get('/auth/:id/:password', function(req, res) {
res.header("Access-Control-Allow-Origin", "*"); //设置跨域访问
res.send({id:req.params.id, name: req.params.password});
}); app.listen(3000);
console.log('Listening on port 3000...');

参考: 
http://m.blog.csdn.net/blog/marujunyy/8852017 
http://www.tuicool.com/articles/vYBR3y

  • 方案二:
var http=require("http");
var server=http.createServer(function(req,res){
if(req.url!=="/favicon.ico"){
res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost:1234"});
res.write("你好啊!");
}
res.end();
});
server.listen(3000,"localhost",function(){
console.log("开始监听...");
});
var http=require("http");
var server=http.createServer(function(req,res){
if(req.url!=="/favicon.ico"){
//res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost:1234"});
res.statusCode=200;
res.sendDate=false;
res.setHeader("Content-Type","text/plain");
res.setHeader("Access-Control-Allow-Origin","http://localhost:63342");
res.write("你好啊!");
}
res.end();
});
server.listen(3000,"localhost",function(){
console.log("开始监听...");
});

参考: 
http://www.jb51.net/article/57874.htm

AJAX 跨域 :Access-Control-Allow-Origin的更多相关文章

  1. 解决js ajax跨越请求 Access control allow origin 异常

    // 解决跨越请求的问题 response.setHeader("Access-Control-Allow-Origin", "*");

  2. java、ajax 跨域请求解决方案('Access-Control-Allow-Origin' header is present on the requested resource. Origin '请求源' is therefore not allowed access.)

      1.情景展示 ajax调取java服务器请求报错 报错信息如下: 'Access-Control-Allow-Origin' header is present on the requested ...

  3. 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin.

    一:使用jsonp格式, 如jquery中ajax请求参数   dataType:'JSONP'. <html> <head> <title>title</t ...

  4. (转)AJax跨域:No 'Access-Control-Allow-Origin' header is present on the requested resource

    在本地用ajax跨域访问请求时报错: No 'Access-Control-Allow-Origin' header is present on the requested resource. Ori ...

  5. ajax跨域问题Access-Control-Allow-Origin

    Access control allow origin直译过来就是"访问控制允许同源",这是由于ajax跨域访问引起的.所谓跨域就是,在a.com域下,访问b.com域下的资源:出 ...

  6. 浏览器同源策略与ajax跨域方法汇总

    原文 什么是同源策略 如果你进行过前端开发,肯定或多或少会听说过.接触过所谓的同源策略.那么什么是同源策略呢? 要了解同源策略,首先得理解“源”.在这个语境下,源(origin)其实就是指的URL.所 ...

  7. Access control allow origin 简单请求和复杂请求

    原文地址:http://blog.csdn.net/wangjun5159/article/details/49096445 错误信息: XMLHttpRequest cannot load http ...

  8. AJAX跨域POST发送json时,会先发送一个OPTIONS预请求

    我们会发现,在很多post,put,delete等请求之前,会有一次options请求. 根本原因就是,W3C规范这样要求了!在跨域请求中,分为简单请求(get和部分post,post时content ...

  9. Ajax跨域、Json跨域、Socket跨域和Canvas跨域等同源策略限制的解决方法

    同源是指同样的协议.域名.port,三者都同样才属于同域.不符合上述定义的请求,则称为跨域. 相信每一个开发者都曾遇到过跨域请求的情况,尽管情况不一样,但问题的本质都能够归为浏览器出于安全考虑下的同源 ...

  10. 基于.Net Framework 4.0 Web API开发(5):ASP.NET Web APIs AJAX 跨域请求解决办法(CORS实现)

    概述:  ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是在使用API的时候总会遇到跨域请求的问题,特 ...

随机推荐

  1. spring data redis使用示例

    1. 配置依赖文件 <dependencies> <dependency> <groupId>org.springframework.data</groupI ...

  2. Booting ARM Linux

    来源:linux-2.6.30.4/Documentation/arm/Booting ARM Linux Booting ARM Linux            ================= ...

  3. C#之结构化异常处理

    1.异常处理4要素 一个表示异常详细信息的类类型: 一个向调用者引发异常类实例的成员: 调用者的一段调用异常成员的代码块: 调用者的一段处理将要发生异常的代码块:

  4. 基调(听云)app,服务器,质量测试手段

    1. 网络质量检测 http://www.tingyun.com/tingyun_network.html

  5. Linux 内存管理子系统

    一.内存管理子系统 1 . 内存管理模块 功能: 虚拟地址到物理地址的映射 32位系统访问的地址一共4G: 0-3G : 应用程序 3G+896M :直接映射 , Vmollat区,永久内存映射,固定 ...

  6. SubLime3 Emmet插件终极教程

    当我们在手写HTML.CSS等 页面的时候,这款神级插件是不可多得的神器 1.官方示例:http://docs.emmet.io/cheat-sheet/ 2.第三方示例:http://www.w3c ...

  7. HTML常见标签

    标题:h1.h2.h3.h4.h5.... 段落:p 换行:br 容器:div.span(用来容纳其他标签) 表格:table.tr.td 列表:ul.ol.li 图片:img 表单:input 链接 ...

  8. 关于类似(i++)+(++i)

    这是一个“然并卵”的问题,因为没有人愿意在代码中给自己找这种麻烦,看到书上讲到这个问题忍不住想顺势总结下,就从表达式说起吧. 在js中,同一般的语言一样,表达式分很多种. 对象和数组的初始化表达式:即 ...

  9. Table of Contents - jBPM

    Getting Started jBPM Installer Eclipse 安装 jBPM 插件 jBPM Core

  10. 手把手教你认识并搭建Nginx

    手把手教你认识并搭建Nginx Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor ...