2015-01-06 29 views
0

有一些实验来构建JavaScript应用程序,而不支持任何服务器端语言,如php,python ...只是与网络数据库层api的JavaScript。使用orchestrate.io,但HTML5 CORS需要严格的标题响应,如访问控制 - 允许来源。有没有办法建立一种应用程序步入DbaaS?Javascript应用程序避免服务器端语言

例如nginx配置为只运行www/index.html。 我们需要通过HTTP使用REST API获取.json数据。这是我们的博客文章。 JSON-P不能发送http头(?)。

谁知道这个?

设置:

nginx的

server { 
    ... 
    root /usr/local/www 
    index index.html 
    ... 

} 

的index.html

function createCORSRequest(method, url) { 
    var xhr = new XMLHttpRequest(); 
    if ("withCredentials" in xhr) { 

    // Check if the XMLHttpRequest object has a "withCredentials" property. 
    // "withCredentials" only exists on XMLHTTPRequest2 objects. 
    xhr.open(method, url, true); 

    } else if (typeof XDomainRequest != "undefined") { 

    // Otherwise, check if XDomainRequest. 
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests. 
    xhr = new XDomainRequest(); 
    xhr.open(method, url); 

    } else { 

    // Otherwise, CORS is not supported by the browser. 
    xhr = null; 

    } 
    return xhr; 
} 

var xhr = createCORSRequest('GET', url); 
if (!xhr) { 
    throw new Error('CORS not supported'); 
} 
xhr.withCredentials = true; 
var url = 'https://api.service.io/article/1'; 
var xhr = createCORSRequest('GET', url); 

xhr.onload = function() { 
    var responseText = xhr.responseText; 
    console.log(responseText); 
// process the response. 
}; 

xhr.onerror = function() { 
    console.log('There was an error!'); 
}; 

xhr.send(); 

,我需要发送基本身份验证的HTTP头......这就是所有

+0

我不确定你的意思。这是您使用CORS orchestrate.io时遇到的特定问题吗?你能提供更多关于你设置的所有信息吗? – Stratus3D

+0

这不是一个管弦乐的心愿单问题。有关将纯JavaScript与DBaas集成的更多信息。安装程序非常简单,就像从浏览器加载html一样。 –

+2

我还是很困惑。 DBaas是数据库即服务的通用术语。也许您可以提供有关您的设置的详细信息(例如,Web服务器配置,Javascript代码示例)。这真的有助于看到一些示例代码在问题中产生了特定的错误。 – Stratus3D

回答

0

Nginx的proxy module可以做到这一点:它捕捉你的要求t与一个普通的XMLHTTPRequest到您选择的某个本地(相同来源)路径,并将其代理到一个远程服务,添加服务需要的任何头文件和proxy_set_header指令。

例如,我做代理请求只有几行Mailgun服务:

location = /mailgun-send 
{ 
    proxy_pass https://api.mailgun.net/v2/mg.inshaker.ru/messages; 
    proxy_set_header 'Host' 'api.mailgun.net'; 
    proxy_set_header 'Authorization' 'Basic YXB3lzbWJlaTVq2N1ZjJ6NG1MDh5aDd0epOmtleS0zM4NjRseXVhNw=='; 
} 

,然后就$.ajax('/mailgun-send')

相关问题