Apache和Nodejs跨域AJAX的问题 [英] Apache and Nodejs cross domain ajax issue

查看:251
本文介绍了Apache和Nodejs跨域AJAX的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作中,我用了两个服务器托管在同一台机器上的应用程序,一个是Apache的工作,并会基本主机服务的PHP页面和对方nodejs的REST API的沟通,整个应用程序建立在骨干/提线木偶/ requirejs /引导。

即将到如此地步,我正常的网页加载来自Apache服务器一样,

  http://192.168.20.62/project/design.php
 

和我已经配置了我的模型是这样,

 定义(['骨干'],功能(骨干){
使用严格的;

返回Backbone.Model.extend({
    网址:http://192.168.20.62:9847/page
    默认值:{
        ...
    }
});

});
 

当我尝试保存模型,我从阿贾克斯跨域调用的问题,痛苦,我结束了错误我节省通信,以下为节点/ EX preSS服务器,

  VAR EX preSS =要求('/根/ node_modules / EX preSS');


VAR应用= EX preSS();

app.configure(函数(){
    app.use(如press.json());
    app.use(如press.urlen codeD());
    app.use(功能(REQ,水库,下一个){
        res.setHeader(访问控制 - 允许 - 原产地,http://192.168.20.62:9847');
        res.setHeader(访问控制 - 允许 - 方法,GET,POST,OPTIONS,PUT,补丁,删除);
        res.setHeader(访问控制 - 允许 - 头,X-要求,通过,内容型);
        res.setHeader(访问控制 - 允许 - 凭据,真正的);
        下一个();
    });

})

app.post('/页',功能(请求,响应){

    执行console.log(request.body);
    response.send(request.body);

});

app.listen(9847);
 

,你可以看到在

我已经写在服务器code一些补丁,但是还是一样,我也已经添加的.htaccess在两者的根目录

  http://192.168.20.62
 

  http://192.168.20.62:9847
 

与下面的code,

 头添加访问控制 - 允许 - 起源*
头添加访问控制 - 允许 - 头的由来,X-要求,用,内容类型
头添加访问控制 - 允许 - 方法PUT,GET,POST,DELETE,OPTIONS
 

但事情并没有帮助的,反正,如果我通过禁用Web安全,然后的事情是工作正常运行的Chrome。

的chrome.exe - 禁用网络安全

你们可以请帮我解决这个难题,先谢谢了。

以下是从铬错误消息JavaScript控制台

 选项http://192.168.20.62:9847/page产地http://192.168.20.62不受访问控制 - 允许 - 产地允许的。 jQuery的-2.0.3.min.js:6

XMLHtt prequest无法加载http://192.168.20.62:9847/page。原产地http://192.168.20.62不受访问控制 - 允许 - 原产地允许的。
 

解决方案

哎呀我弄清楚这个问题,我已经安装错误的URL的白名单。

res.setHeader(访问控制 - 允许 - 原产地,http://192.168.20.62');

我要白名单这是产生跨域源URL,这是唯一的改变已经做的,使事情运转。

另外这款具有浏览器版本的影响,当我已经张贴了这个问题,我检查与Firefox版本24.0。*当我升级25.0已经停止出人意料地产生跨域错误。但还是镀铬给人自然跨域错误,当我读到铬的错误信息小心我才知道,我已经列入白名单错误的URL。

XMLHtt prequest无法加载http://192.168.20.62:9847/page。在访问控制 - 允许 - 原产地只有白名单http://192.168.20.62:9847。原点'http://192.168.20.62'不是在列表中,并且因此不允许访问。

I am working on application in which I have used two servers hosted on same machine, one is apache which will work as basic host for serving php pages and other side nodejs for communication of rest api, whole application build upon backbone/marionette/requirejs/bootstrap.

Coming to the point, my normal pages are load from apache server like,

http://192.168.20.62/project/design.php

and I have configured my model like this,

define(['backbone'],function(Backbone){
'use strict';

return Backbone.Model.extend({
    url:"http://192.168.20.62:9847/page",
    defaults: {
        ...
    }
});

});

when I try to save model I am suffering from problem of ajax cross domain call and I am ended up with error in my save communication, following is the node/express server,

var express = require('/root/node_modules/express');


var app = express();

app.configure(function () {
    app.use(express.json());
    app.use(express.urlencoded());
    app.use(function (req, res, next) {
        res.setHeader('Access-Control-Allow-Origin', 'http://192.168.20.62:9847');
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
        res.setHeader('Access-Control-Allow-Credentials', true);
        next();
    });

})

app.post('/page', function(request, response){

    console.log(request.body);
    response.send(request.body);

});

app.listen(9847);

as you can see I have already written some patch in server code, but still the same, also I have added .htaccess at root level of both at

http://192.168.20.62 

and

http://192.168.20.62:9847 

with the following code,

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

but things are not helping in anyways, if I run chrome by disabling web security then thing are working properly.

chrome.exe --disable-web-security

Can you guys please help me to solve this puzzle, thanks in advance.

following is the error message from chrome javascript console

OPTIONS http://192.168.20.62:9847/page Origin http://192.168.20.62 is not allowed by Access-Control-Allow-Origin. jquery-2.0.3.min.js:6

XMLHttpRequest cannot load http://192.168.20.62:9847/page. Origin http://192.168.20.62 is not allowed by Access-Control-Allow-Origin. 

解决方案

Oops I figure out the problem, I have setup wrong url for white listing.

res.setHeader('Access-Control-Allow-Origin', 'http://192.168.20.62');

I have to whitelist the source URL which was generating cross domain, this was the only change have to do to make thing running.

Also this has browser version impact, when I have posted this issue, I was checking with firefox version 24.0.* and when I upgrade 25.0 it has stopped generating cross domain error surprisingly. But still Chrome giving natural cross domain error, when I read error message of chrome carefully I come to know that I have whitelisted wrong url.

XMLHttpRequest cannot load http://192.168.20.62:9847/page. The 'Access-Control-Allow-Origin' whitelists only 'http://192.168.20.62:9847'. Origin 'http://192.168.20.62' is not in the list, and is therefore not allowed access.

这篇关于Apache和Nodejs跨域AJAX的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆