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

abergel
No idea, but your intent is really valuable

Alexandre



Le 23 sept. 2011 à 11:56, Bernat Romagosa <[hidden email]> a écrit :

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

Richard Durr-2
In reply to this post by Bernat Romagosa
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.

Reply | Threaded
Open this post in threaded view
|

Re: response to an AJAX POST request

Richard Durr-2
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.


Reply | Threaded
Open this post in threaded view
|

Re: response to an AJAX POST request

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: 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.