当前位置: 动力学知识库 > 问答 > 编程问答 >

web services - Using javascript to call an expressjs restful webservice => result always empty respons

问题描述:

Purpose:
1) Create an website witch runs on a NodeJs webserver.

(expressjs, stylus, jade) + NodeJS

2) Create an restful webservice on an NodeJs webserver

(expressjs) + NodeJS

3) The website calls an restful web service entry

(javascript file)


With the ubuntu terminal i start the nodeJs for the website an another one for the rest

webservice.Testing the restful webservice (in browser):

Url:localhost:1337/wines/

Data result is:

[

{

"name": "New wine",

"Year": "2012",

"_id": "50e8255197f0b5260f000001"

}

]

Testing the website

Url: localhost:3000/

This is my javascript file used in the website, here i want to call the

rest url localhost:1337/wines/ en get the result data.

alert('hello!'); (popup)

var xhr = new XMLHttpRequest();

xhr.open('GET', 'http://localhost:1337/wines/');

xhr.onreadystatechange = function () {

if (this.status == 200 && this.readyState == 4) {

console.log('response: ' + this.responseText);

alert("Yes");

}

};

xhr.send();

In the terminals i see that the GET is performed:

Restful terminal:

GET /wines/ 200 3ms - 93

Website terminal:

GET / 200 11ms

GET /JavaScript/script.js 304 1ms

GET /stylesheets/style.css 304 1ms

When i debug i goes like this:

var xhr = new XMLHttpRequest(); OK

xhr.open('GET', 'http://localhost:1337/wines/'); OK

xhr.send(); OK

xhr.onreadystatechange = function () Jump into call back OK

but 'readystate=4 but this.status = 0' and i do not gave an result

In the end i get this error:

Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)


Result info from firebug (used to debug this java script)

Content-Type application/json; charset=utf-8

Date Sun, 06 Jan 2013 04:15:07 GMT

X-Powered-By Express

Request Headers

Accept text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Accept-Encoding gzip, deflate

Accept-Language en-us,en;q=0.5

Connection keep-alive

Host localhost:1337

Origin //localhost:3000

Referer //localhost:3000/

User-Agent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101
Firefox/13.0.1


I searched stack overflow and it could be an 'cross domain issue', therefore i should

add some javascript to my restful webservice:

var express = require('express'),

wine = require('./routes/wines');

var app = express();

app.configure(function () {

app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */

app.use(express.bodyParser());

//Added part

app.all('/', function(req, res, next) {

res.header("Access-Control-Allow-Origin", "*");

res.header("Access-Control-Allow-Headers", "*");

res.header("Access-Control-Allow-Methods", "XPOST, GET, OPTIONS");

res.header("Access-Control-Max-Age", "1000");

next();

});

//Added part

});

app.get('/wines', wine.findAll);

app.get('/wines/:id', wine.findById);

app.post('/wines', wine.addWine);

app.put('/wines/:id', wine.updateWine);

app.delete('/wines/:id', wine.deleteWine);

app.listen(1337);

console.log('Listening on port 1337...');


Still no result, anyone an idea how to fix this issue? Many thanx

网友答案:

You have to register the event handler before calling open().

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if (this.status == 200 && this.readyState == 4) {
    console.log('response: ' + this.responseText);
    alert("Yes");

  }
};
xhr.open('GET', 'http://localhost:1337/wines/');
xhr.send();
分享给朋友:
您可能感兴趣的文章:
随机阅读: