response to an AJAX POST request

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

response to an AJAX POST request

Bernat Romagosa
Hi lists,

I'm trying to get Amber and Pharo to talk to each other by using Ajax. So far, using an Ajax wrapper that Laurent wrote, I managed to get Pharo to receive a message from Amber by doing:

|ajax|
ajax := (Ajax url: 'http://localhost:4001/amber/').
ajax options
at: 'type' put: 'POST';
at: 'data' put: 'Hi Pharo!';
at: 'timeout' put: 500.
ajax onSuccessDo: [console log: 'ok!'];
onErrorDo: [console log: 'not so ok...'].
ajax send

This is captured by a Swazoo resource, that does the following:

answerTo: aRequest 
| response |
[aRequest postData stream anyDataReady] 
whileTrue:
[Transcript show: aRequest postData stream next].

response := HTTPResponse ok.
response
contentType: 'text/plain';
entity: 'Done!'.
^response

My intention here is just to read the message data and return an OK response, but for some reason stream anyDataReady keeps evaluating to true even when there's no more data to read, so the code underneath is never executed. The Transcript shows 'Hi Pharo!' though, so I'm sure the request is being received and read.

Any light shed on this would be of great help...

Thanks!

--
Bernat Romagosa.
Reply | Threaded
Open this post in threaded view
|

Re: response to an AJAX POST request

Bernat Romagosa
Ok, so I've just learnt I should be doing:

answerTo: aRequest 
| response |
aRequest postDataKeys do: [:eachKey | Transcript show: (aRequest postDataAt: eachKey) value].
response := HTTPResponse ok.
^response

But then the client's javascript console shows: "XMLHttpRequest cannot load http://localhost:4001/amber/. Origin http://localhost:4000 is not allowed by Access-Control-Allow-Origin."

Ideas?

2011/9/23 Bernat Romagosa <[hidden email]>
Hi lists,

I'm trying to get Amber and Pharo to talk to each other by using Ajax. So far, using an Ajax wrapper that Laurent wrote, I managed to get Pharo to receive a message from Amber by doing:

|ajax|
ajax := (Ajax url: 'http://localhost:4001/amber/').
ajax options
at: 'type' put: 'POST';
at: 'data' put: 'Hi Pharo!';
at: 'timeout' put: 500.
ajax onSuccessDo: [console log: 'ok!'];
onErrorDo: [console log: 'not so ok...'].
ajax send

This is captured by a Swazoo resource, that does the following:

answerTo: aRequest 
| response |
[aRequest postData stream anyDataReady] 
whileTrue:
[Transcript show: aRequest postData stream next].

response := HTTPResponse ok.
response
contentType: 'text/plain';
entity: 'Done!'.
^response

My intention here is just to read the message data and return an OK response, but for some reason stream anyDataReady keeps evaluating to true even when there's no more data to read, so the code underneath is never executed. The Transcript shows 'Hi Pharo!' though, so I'm sure the request is being received and read.

Any light shed on this would be of great help...

Thanks!

--
Bernat Romagosa.



--
Bernat Romagosa.
Reply | Threaded
Open this post in threaded view
|

Re: response to an AJAX POST request

radoslav hodnicak-2
Ajax requests are only allowed to connect to the url of the page they
run on (that's a security limitation/feature of all web browsers).
Since the port is a part of the url, localhost:4000 and localhost:4001
is not the same. The easiest solution is to hide both behind a web
server and proxy the requests as localhost/pharo and localhost/amber
or whatever

rado

On Sep 23, 5:56 pm, Bernat Romagosa <[hidden email]>
wrote:

> Ok, so I've just learnt I should be doing:
>
> answerTo: aRequest
> | response |
> aRequest postDataKeys do: [:eachKey | Transcript show: (aRequest postDataAt:
> eachKey) value].
>  response := HTTPResponse ok.
> ^response
>
> But then the client's javascript console shows: "XMLHttpRequest cannot loadhttp://localhost:4001/amber/. Originhttp://localhost:4000<http://localhost/>is not allowed by
> Access-Control-Allow-Origin.
> "
>
> Ideas?
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] response to an AJAX POST request

Bernat Romagosa
In reply to this post by Bernat Romagosa
Thanks guys!

I read that Mozilla link, and it seems you just need to tell the server to respond that the origin URL is allowed to send requests to the server. I asked Janko how to do that and the following line was enough:

response addHeaderName: 'Access-Control-Allow-Origin' value: 'http://localhost:4000/'.

Now Amber can send data to Pharo and receive a valid response.

Cheers,

2011/9/23 Richard Durr <[hidden email]>
Likely the problem, see: http://meteatamel.wordpress.com/2011/02/11/access-control-allow-origin/
and from there on: https://developer.mozilla.org/En/HTTP_Access_Control


On Fri, Sep 23, 2011 at 11:40 PM, Richard Durr <[hidden email]> wrote:
Maybe the Access-Control-Allow-Origin blocks because of the different ports?

On Fri, Sep 23, 2011 at 5:56 PM, Bernat Romagosa <[hidden email]> wrote:
Ok, so I've just learnt I should be doing:

answerTo: aRequest 
| response |
aRequest postDataKeys do: [:eachKey | Transcript show: (aRequest postDataAt: eachKey) value].
response := HTTPResponse ok.
^response

But then the client's javascript console shows: "XMLHttpRequest cannot load http://localhost:4001/amber/. Origin http://localhost:4000 is not allowed by Access-Control-Allow-Origin."

Ideas?

2011/9/23 Bernat Romagosa <[hidden email]>
Hi lists,

I'm trying to get Amber and Pharo to talk to each other by using Ajax. So far, using an Ajax wrapper that Laurent wrote, I managed to get Pharo to receive a message from Amber by doing:

|ajax|
ajax := (Ajax url: 'http://localhost:4001/amber/').
ajax options
at: 'type' put: 'POST';
at: 'data' put: 'Hi Pharo!';
at: 'timeout' put: 500.
ajax onSuccessDo: [console log: 'ok!'];
onErrorDo: [console log: 'not so ok...'].
ajax send

This is captured by a Swazoo resource, that does the following:

answerTo: aRequest 
| response |
[aRequest postData stream anyDataReady] 
whileTrue:
[Transcript show: aRequest postData stream next].

response := HTTPResponse ok.
response
contentType: 'text/plain';
entity: 'Done!'.
^response

My intention here is just to read the message data and return an OK response, but for some reason stream anyDataReady keeps evaluating to true even when there's no more data to read, so the code underneath is never executed. The Transcript shows 'Hi Pharo!' though, so I'm sure the request is being received and read.

Any light shed on this would be of great help...

Thanks!

--
Bernat Romagosa.



--
Bernat Romagosa.





--
Bernat Romagosa.
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] response to an AJAX POST request

Bernat Romagosa
(btw I documented the whole process here: http://asmalltalkbytheseaside.com/)

2011/9/26 Bernat Romagosa <[hidden email]>
Thanks guys!

I read that Mozilla link, and it seems you just need to tell the server to respond that the origin URL is allowed to send requests to the server. I asked Janko how to do that and the following line was enough:

response addHeaderName: 'Access-Control-Allow-Origin' value: 'http://localhost:4000/'.

Now Amber can send data to Pharo and receive a valid response.

Cheers,


2011/9/23 Richard Durr <[hidden email]>
Likely the problem, see: http://meteatamel.wordpress.com/2011/02/11/access-control-allow-origin/
and from there on: https://developer.mozilla.org/En/HTTP_Access_Control


On Fri, Sep 23, 2011 at 11:40 PM, Richard Durr <[hidden email]> wrote:
Maybe the Access-Control-Allow-Origin blocks because of the different ports?

On Fri, Sep 23, 2011 at 5:56 PM, Bernat Romagosa <[hidden email]> wrote:
Ok, so I've just learnt I should be doing:

answerTo: aRequest 
| response |
aRequest postDataKeys do: [:eachKey | Transcript show: (aRequest postDataAt: eachKey) value].
response := HTTPResponse ok.
^response

But then the client's javascript console shows: "XMLHttpRequest cannot load http://localhost:4001/amber/. Origin http://localhost:4000 is not allowed by Access-Control-Allow-Origin."

Ideas?

2011/9/23 Bernat Romagosa <[hidden email]>
Hi lists,

I'm trying to get Amber and Pharo to talk to each other by using Ajax. So far, using an Ajax wrapper that Laurent wrote, I managed to get Pharo to receive a message from Amber by doing:

|ajax|
ajax := (Ajax url: 'http://localhost:4001/amber/').
ajax options
at: 'type' put: 'POST';
at: 'data' put: 'Hi Pharo!';
at: 'timeout' put: 500.
ajax onSuccessDo: [console log: 'ok!'];
onErrorDo: [console log: 'not so ok...'].
ajax send

This is captured by a Swazoo resource, that does the following:

answerTo: aRequest 
| response |
[aRequest postData stream anyDataReady] 
whileTrue:
[Transcript show: aRequest postData stream next].

response := HTTPResponse ok.
response
contentType: 'text/plain';
entity: 'Done!'.
^response

My intention here is just to read the message data and return an OK response, but for some reason stream anyDataReady keeps evaluating to true even when there's no more data to read, so the code underneath is never executed. The Transcript shows 'Hi Pharo!' though, so I'm sure the request is being received and read.

Any light shed on this would be of great help...

Thanks!

--
Bernat Romagosa.



--
Bernat Romagosa.





--
Bernat Romagosa.



--
Bernat Romagosa.
Reply | Threaded
Open this post in threaded view
|

Re: response to an AJAX POST request

Jacob Wagner

It seems this only works for one web client at a time, any ideas on
how to get multiple web clients to interact with the pharo server?
<zenchess@gmail.com>
Reply | Threaded
Open this post in threaded view
|

Re: response to an AJAX POST request

Bernat Romagosa
I can only think of having a list of allowed clients and generating a different response for each... but I guess that'd be so inefficient.

You should handle this at AmberResource >> answerTo: aRequest

response addHeaderName: 'Access-Control-Allow-Origin' value: 'http://localhost:4000/'.  ← Either change this url for '*' (danger!) or else have a list of allowed clients and check whether the request comes from one of them.

(I don't know the first thing about networks so I might be talking nonsense...)

Cheers!

2011/9/28 Zenchess <[hidden email]>

It seems this only works for one web client at a time, any ideas on
how to get multiple web clients to interact with the pharo server?



--
Bernat Romagosa.