2

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"}
' 
Community
  • 1
  • 1
z--
  • 2,186
  • 17
  • 33

1 Answers1

3

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.

MKroehnert
  • 3,637
  • 2
  • 34
  • 43
  • Thank you, it is your case 1, I have added more details to the question. In the references section I have linked another question which asked this in connection with linking Apache served files to couchDB (all localhost). – z-- Sep 04 '13 at 03:32
  • It is case 1 in terms of setup. couchdb is running on port 5984, amber is running on port 4000. I need to tell the pages served by amber to allow the XMLHttpRequest access to port 5984. So in case of solution it seems to be your case 2; I have to configure the amber-server. – z-- Sep 04 '13 at 06:38
  • @Hannes: Then you must configure the CouchDB server to provide the `Control-Allow-Origin` header as suggested in the question you linked. It is CouchDB that doesn't allow the connection and not the Amber server since the webpage is not served on the CouchDB port. – MKroehnert Sep 04 '13 at 12:10