ajax call to node.js server

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

ajax call to node.js server

laci
Hi,
I am trying to make Ajax calls from amber into an ajax server running
on node.js.
The jquery call succeeds, while the amber call fails.

Fiddler reports that the jquery url is
  http://localhost:8124/?callback=_testcb&_=1319230161161
while the amber url is
  http://localhost:8124/?callback=jQuery16203472660253445833_1319222860388&_=1319232348716

This must be the acuse of the error.

I am launching amber from http://amber-lang.net/
By the way the amber version, which I downloaded from github doesn't
contain the jQuery package. Am I doing something wrong?

Thanks in advance,
 laci

Sources:
The node.js ajax server:
var http = require('http');

http.createServer(function (req, res) {
    console.log('request received');
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('_testcb(\'{"message": "sziasztok!"}\')');
}).listen(8124);

jquery code invoking the node.js server:
$(document).ready(function() {
    $.ajax({
        url: 'http://localhost:8124/',
        dataType: "jsonp",
        jsonpCallback: "_testcb",
        cache: false,
        timeout: 5000,
        success: function(data) {
            $("#test").append(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('error ' + textStatus + " " + errorThrown);
        }
    });
});


The amber code - two version of it - neither of them works:
| url |
url := 'http://localhost:8124/'.
Ajax new
    url: url;
    at: 'dataType' put: 'jsonp';
    at: 'callback' put: '_testcb';
    at: 'timeout' put: 5000;
    onSuccessDo: [ :data | Transcript show: data ];
    onErrorDo: [ :error | Transcript show: 'Request failed!' ];
    send.

| url |
url := 'http://localhost:8124/'.
jQuery ajax: #{
  #url -> url.
  #dataType -> 'jsonp'.
  #callback -> '_testcb'.
  #timeout -> 5000.
  #success ->  [ :data | Transcript show: data ].
  #failure -> [ :error | Transcript show: 'Request failed!' ]
}.
Reply | Threaded
Open this post in threaded view
|

Re: ajax call to node.js server

Nicolas Petton
Hi,

The jQuery binding has been removed a while ago, I'm surprised that it's
still on the website.

With a recent version of Amber, you can do:

jQuery
        ajax: 'path'
        options: aDictionary

According to: http://api.jquery.com/jQuery.ajax/

Cheers,
Nico

On Fri, 2011-10-21 at 14:43 -0700, laci wrote:

> Hi,
> I am trying to make Ajax calls from amber into an ajax server running
> on node.js.
> The jquery call succeeds, while the amber call fails.
>
> Fiddler reports that the jquery url is
>   http://localhost:8124/?callback=_testcb&_=1319230161161
> while the amber url is
>   http://localhost:8124/?callback=jQuery16203472660253445833_1319222860388&_=1319232348716
>
> This must be the acuse of the error.
>
> I am launching amber from http://amber-lang.net/
> By the way the amber version, which I downloaded from github doesn't
> contain the jQuery package. Am I doing something wrong?
>
> Thanks in advance,
>  laci
>
> Sources:
> The node.js ajax server:
> var http = require('http');
>
> http.createServer(function (req, res) {
>     console.log('request received');
>     res.writeHead(200, {'Content-Type': 'text/plain'});
>     res.end('_testcb(\'{"message": "sziasztok!"}\')');
> }).listen(8124);
>
> jquery code invoking the node.js server:
> $(document).ready(function() {
>     $.ajax({
>         url: 'http://localhost:8124/',
>         dataType: "jsonp",
>         jsonpCallback: "_testcb",
>         cache: false,
>         timeout: 5000,
>         success: function(data) {
>             $("#test").append(data);
>         },
>         error: function(jqXHR, textStatus, errorThrown) {
>             alert('error ' + textStatus + " " + errorThrown);
>         }
>     });
> });
>
>
> The amber code - two version of it - neither of them works:
> | url |
> url := 'http://localhost:8124/'.
> Ajax new
>     url: url;
>     at: 'dataType' put: 'jsonp';
>     at: 'callback' put: '_testcb';
>     at: 'timeout' put: 5000;
>     onSuccessDo: [ :data | Transcript show: data ];
>     onErrorDo: [ :error | Transcript show: 'Request failed!' ];
>     send.
>
> | url |
> url := 'http://localhost:8124/'.
> jQuery ajax: #{
>   #url -> url.
>   #dataType -> 'jsonp'.
>   #callback -> '_testcb'.
>   #timeout -> 5000.
>   #success ->  [ :data | Transcript show: data ].
>   #failure -> [ :error | Transcript show: 'Request failed!' ]
> }.


Reply | Threaded
Open this post in threaded view
|

Re: ajax call to node.js server

laci
Hi Nico,

Works just fine. For completness see code below.

Thanks,
 laszlo

| options url |
url := 'http://localhost:8124/'.
options := #{
  #url -> url.
  #dataType -> 'jsonp'.
  #jsonpCallback -> '_testcb'.
  #cache -> false.
  #timeout -> 5000.
  #success ->  [ :data | Transcript show: 'Request successful. Data:
'; show: data ].
  #failure -> [ :error | Transcript show: 'Request failed!' ]
}.
jQuery ajax: url options: options
Reply | Threaded
Open this post in threaded view
|

Re: ajax call to node.js server

Hannes Hirzel
 Laszlo

Thank you for sharing this.  Would it be possible to post the whole
example -- client and server?

--Hannes

On 10/22/11, laci <[hidden email]> wrote:

> Hi Nico,
>
> Works just fine. For completness see code below.
>
> Thanks,
>  laszlo
>
> | options url |
> url := 'http://localhost:8124/'.
> options := #{
>   #url -> url.
>   #dataType -> 'jsonp'.
>   #jsonpCallback -> '_testcb'.
>   #cache -> false.
>   #timeout -> 5000.
>   #success ->  [ :data | Transcript show: 'Request successful. Data:
> '; show: data ].
>   #failure -> [ :error | Transcript show: 'Request failed!' ]
> }.
> jQuery ajax: url options: options
>
Reply | Threaded
Open this post in threaded view
|

Re: ajax call to node.js server

laci
Hannes,

Glad to compile the complete code.

For completeness please find below the
 . node.js server code
 . client code implemented in amber Smalltalk and with jQuery

Prerequisites:
 . availability of node.js environment - http://nodejs.org/
 . test environment: http://amber-lang.net/

Environment: Windows Vista

Steps:
 1. save the server side code in file ajax_server.js
 2. start up the ajax server by executing at command line:
      node ajax_server.js
 3. test the ajax server with URL
      http://localhost:8124/
 4. copy and execute the client-side amber code in amber workspace at
http://amber-lang.net/
 5. check the log in the Transcript

Server side - node.js code:
var http = require('http');

http.createServer(function (req, res) {
    console.log('request received');
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('_testcb(\'{"message": "Hello world!"}\')');
}).listen(8124);

Client side - amber code:
| options url |
url := 'http://localhost:8124/'.
options := #{
  #url -> url.
  #dataType -> 'jsonp'.
  #jsonpCallback -> '_testcb'.
  #cache -> false.
  #timeout -> 5000.
  #success ->  [ :data | Transcript show: 'Request successful. Data:';
show: data ].
  #failure -> [ :error | Transcript show: 'Request failed!' ] }.
jQuery ajax: url options: options

Client side - jQuery code:
$(document).ready(function() {
    $.ajax({
        url: 'http://localhost:8124/',
        dataType: "jsonp",
        jsonpCallback: "_testcb",
        cache: false,
        timeout: 5000,
        success: function(data) {
            $("#test").append(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('error ' + textStatus + " " + errorThrown);
        }
    });
});