简体   繁体   English

Javascript应用避免使用服务器端语言

[英]Javascript app avoid server-side languages

Have some experiments to build javascript app without support any server-side languages like php, python ... just javascript with a network database layer api. 有一些实验来构建javascript应用,而不支持任何服务器端语言,例如php,python ...只是具有网络数据库层api的javascript。 Used orchestrate.io, but the HTML5 CORS require strict headers response like Access-Control-Allow-Origin . 使用了orchestrate.io,但是HTML5 CORS需要严格的标头响应,例如Access-Control-Allow-Origin Is there a way to build a kind of application stepping to DbaaS ? 有没有一种方法可以构建到DbaaS的应用程序?

For example nginx is configured to run only www/index.html . 例如,nginx配置为仅运行www / index.html We need to get .json data using REST API through the HTTP. 我们需要通过HTTP使用REST API获取.json数据。 This is our blog articles. 这是我们的博客文章。 JSON-P cant send http headers(?). JSON-P无法发送http标头(?)。

Who knows this ? 谁知道呢?

Setup: 设定:

nginx Nginx的

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

}

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();

and i need to send basic auth http headers ... thats all 而且我需要发送基本的auth http标头...多数民众赞成

Nginx proxy module can do just that: it catches your request with an ordinary XMLHTTPRequest to some local (same origin) path of your choice and proxies it to a remote service adding any headers the service needs with proxy_set_header directive. Nginx 代理模块可以做到这一点:它使用普通的XMLHTTPRequest捕获您选择的某个本地(相同来源)路径的请求,并将其代理到远程服务,并使用proxy_set_header指令添加该服务所需的任何报头。

For example, I do proxy requests to a Mailgun service with just few lines: 例如,我仅用几行就对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==';
}

and then just $.ajax('/mailgun-send') . 然后只是$.ajax('/mailgun-send')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM