Вопрос

The Amber Smalltalk IDE works with a server written in nodejs. How can I configure the server that XMLHttpRequests going to a different port of the same domain are allowed?

The default access to Amber is

http://127.0.0.1:4000/

And to retrieve and store JSON data I want to use a couchDB instance (default port is 5984)

| req |

req := XMLHttpRequest new.

req open: 'GET' url: 'http://127.0.0.1:5984/' asynchronous: false.

req send: ''.

The problem

Access is not possible because of cross-domain access policy.

Notes

The server is called from

amber-master\bin\server.bat

The server is in

amber-master\cli\js\amber-cli.js

The client is Firefox which should allow that the XMLHttpRequest objects may access a different port provided the server indicates this with an 'Access-Control-Allow-Origin header'.

References

http://www.w3.org/TR/cors/#access-control-allow-origin-response-header

CouchDB cross-domain access from XMLHttpRequest?

After answer by MKroenert

I upgraded to version 1.4.0 of CouchDB and adapted the local.ini file to allow for CORS (C:\Program Files\Apache Software Foundation\CouchDB\etc\couchdb\local.ini)

[httpd]
enable_cors = true

[cors]
origins = *

More on http://wiki.apache.org/couchdb/CORS In particular how to limit access.

3.12.1. Enabling CORS http://docs.couchdb.org/en/latest/configuring.html

Then after restarting the couchDB service the following code snippet works fine in an Amber Smalltalk workspace

| req colordict mimeType |
colordict := HashedCollection new.

colordict at: 'red' put: 'rot'.
colordict at: 'blue' put: 'blau'.
colordict at: 'yellow' put: 'gelb'.

req := XMLHttpRequest new.
req open: 'PUT' 
    url: 'http://localhost:5984/components/test2' asynchronous: false.

mimeType :='application/json'.

req setRequestHeader:  'Content-Type'  mimeType: mimeType. 
req send: (JSON stringify: colordict).
req responseText  

A 'printit' gives back

'{"ok":true,"id":"test2","rev":"1-8d2356ebdbabdd87a35e0ae3b137bdb5"}
' 
Это было полезно?

Решение

If I understand your problem correctly you try to access a resource on 127.0.0.1:5984 from within an Amber program.

Since it is not mentioned in the question there are two possible cases in this question:

  1. A non Amber server is running on port 5984.

    In this case it is the responsibility of the specific server running on port 5984 to provide the Access-Control-Allow-Origin: * header and is not a problem with the Amber server.

  2. Another Amber server is running on port 5984.

    In this case we do not currently implement sending the Access-Control-Allow-Origin: * header. The Amber server is meant to be a simple development server and should not be used for deployment.

However, if there is a specific use-case where sending this header is necessary we can discuss this on the Amber mailinglist or create an issue on the GitHub tracker and mark it as a feature request.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top