Why the image is not shown?
up vote
0
down vote
favorite
I'm trying to get image (jpg file) from node.js
and display it in html tag (img
), but the picture is not shown (as you can see:).
My node.js which handle the request looks:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended : true));
app.get('/pictureItem', function (req, res)
// imagesNames[0].name - contains the name of the image to send (jpg file)
res.sendFile('/images/' + imagesNames[0].name, root: __dirname )
)
and my js code looks:
$.get("/pictureItem", function(data, status)
console.log("got image");
$("#imageId").attr("src", data);
)
what am I missing ?
javascript jquery html node.js
add a comment |
up vote
0
down vote
favorite
I'm trying to get image (jpg file) from node.js
and display it in html tag (img
), but the picture is not shown (as you can see:).
My node.js which handle the request looks:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended : true));
app.get('/pictureItem', function (req, res)
// imagesNames[0].name - contains the name of the image to send (jpg file)
res.sendFile('/images/' + imagesNames[0].name, root: __dirname )
)
and my js code looks:
$.get("/pictureItem", function(data, status)
console.log("got image");
$("#imageId").attr("src", data);
)
what am I missing ?
javascript jquery html node.js
3
What is the value ofdata
that is being returned? It almost looks like you are returning the byte data of the file itself
– Taplar
Nov 9 at 20:09
What is the file that you're sending on request? Is it an.html
file? Could you specify where your front-endjs
code is located
– zfrisch
Nov 9 at 20:13
If, as Taplar surmises you are sending the byte data of the image, you're doing it wrong! All you need to display the image is to update the dom with the new image metadata, and the browser will fetch & render the image itself. Even if you did want the byte data, say to paint into a html5 canvas, it does not look like your client side 'get' request is actually asking for a file. There is no indication that it populates the 'imageNames' parameter. And, in turn, it looks like the imageNames parameter is expected in the body, which is not standard procedure for a GET request!
– bluejack
Nov 9 at 20:26
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to get image (jpg file) from node.js
and display it in html tag (img
), but the picture is not shown (as you can see:).
My node.js which handle the request looks:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended : true));
app.get('/pictureItem', function (req, res)
// imagesNames[0].name - contains the name of the image to send (jpg file)
res.sendFile('/images/' + imagesNames[0].name, root: __dirname )
)
and my js code looks:
$.get("/pictureItem", function(data, status)
console.log("got image");
$("#imageId").attr("src", data);
)
what am I missing ?
javascript jquery html node.js
I'm trying to get image (jpg file) from node.js
and display it in html tag (img
), but the picture is not shown (as you can see:).
My node.js which handle the request looks:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended : true));
app.get('/pictureItem', function (req, res)
// imagesNames[0].name - contains the name of the image to send (jpg file)
res.sendFile('/images/' + imagesNames[0].name, root: __dirname )
)
and my js code looks:
$.get("/pictureItem", function(data, status)
console.log("got image");
$("#imageId").attr("src", data);
)
what am I missing ?
javascript jquery html node.js
javascript jquery html node.js
edited Nov 9 at 20:16
asked Nov 9 at 20:08
user3668129
87311225
87311225
3
What is the value ofdata
that is being returned? It almost looks like you are returning the byte data of the file itself
– Taplar
Nov 9 at 20:09
What is the file that you're sending on request? Is it an.html
file? Could you specify where your front-endjs
code is located
– zfrisch
Nov 9 at 20:13
If, as Taplar surmises you are sending the byte data of the image, you're doing it wrong! All you need to display the image is to update the dom with the new image metadata, and the browser will fetch & render the image itself. Even if you did want the byte data, say to paint into a html5 canvas, it does not look like your client side 'get' request is actually asking for a file. There is no indication that it populates the 'imageNames' parameter. And, in turn, it looks like the imageNames parameter is expected in the body, which is not standard procedure for a GET request!
– bluejack
Nov 9 at 20:26
add a comment |
3
What is the value ofdata
that is being returned? It almost looks like you are returning the byte data of the file itself
– Taplar
Nov 9 at 20:09
What is the file that you're sending on request? Is it an.html
file? Could you specify where your front-endjs
code is located
– zfrisch
Nov 9 at 20:13
If, as Taplar surmises you are sending the byte data of the image, you're doing it wrong! All you need to display the image is to update the dom with the new image metadata, and the browser will fetch & render the image itself. Even if you did want the byte data, say to paint into a html5 canvas, it does not look like your client side 'get' request is actually asking for a file. There is no indication that it populates the 'imageNames' parameter. And, in turn, it looks like the imageNames parameter is expected in the body, which is not standard procedure for a GET request!
– bluejack
Nov 9 at 20:26
3
3
What is the value of
data
that is being returned? It almost looks like you are returning the byte data of the file itself– Taplar
Nov 9 at 20:09
What is the value of
data
that is being returned? It almost looks like you are returning the byte data of the file itself– Taplar
Nov 9 at 20:09
What is the file that you're sending on request? Is it an
.html
file? Could you specify where your front-end js
code is located– zfrisch
Nov 9 at 20:13
What is the file that you're sending on request? Is it an
.html
file? Could you specify where your front-end js
code is located– zfrisch
Nov 9 at 20:13
If, as Taplar surmises you are sending the byte data of the image, you're doing it wrong! All you need to display the image is to update the dom with the new image metadata, and the browser will fetch & render the image itself. Even if you did want the byte data, say to paint into a html5 canvas, it does not look like your client side 'get' request is actually asking for a file. There is no indication that it populates the 'imageNames' parameter. And, in turn, it looks like the imageNames parameter is expected in the body, which is not standard procedure for a GET request!
– bluejack
Nov 9 at 20:26
If, as Taplar surmises you are sending the byte data of the image, you're doing it wrong! All you need to display the image is to update the dom with the new image metadata, and the browser will fetch & render the image itself. Even if you did want the byte data, say to paint into a html5 canvas, it does not look like your client side 'get' request is actually asking for a file. There is no indication that it populates the 'imageNames' parameter. And, in turn, it looks like the imageNames parameter is expected in the body, which is not standard procedure for a GET request!
– bluejack
Nov 9 at 20:26
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Your '/pictureItem'
route in Express sends an image.
Your ajax call in your client code seems to be expecting to get an URL back, not an image:
$.get("/pictureItem", function(data, status)
console.log("got image");
$("#imageId").attr("src", data);
);
The usual way you would do this is:
- Set
$("#imageId").attr("src", data);
to an URL that your server knows how to serve the image for. - This will cause the browser to then request that URL from your server.
- When your server gets that image request, it will send the image back.
- The browser will display the image it got back from the server.
I'm not quite sure what the overall problem is you're trying to solve here (you don't show the overall logic of the operation), but you could just do this:
$("#imageId").attr("src", "/pictureItem");
And, then your existing server route would return the desired image when the browser requests the image from the /pictureItem
route.
If I have multiple images, and I want to serve all of them with one request (one get request from server), how can I set the "data" with one string and parse it in server in order to send the right image ?
– user3668129
Nov 9 at 20:40
1
@user3668129 - You don't typically serve multiple images with one request unless you're playing tricks with sprites (which are sub-images buried in one master image). The browser requests an image, the server sends that on image. If the browser wants more than one image, it requests them each separately. Sorry, but I still don't understand what real overall problem you're trying to solve here. Please edit your question to go back a few steps and describe what you're really trying to accomplish here.
– jfriend00
Nov 9 at 20:43
add a comment |
up vote
-1
down vote
Have you defined the middleware for static files? If yes you could define a GET
method which retrieves the path to a specific image and return that so you can attach it to the src
attribute.
Your pictureItem
endpoint is return the image file itself. Whereas your src attribute expects a string so it can fetch the image itself.
server.js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended : true));
app.use('/images', express.static(__dirname + '/Images'));
app.get('/pictureItem', function (req, res)
// returns a string as '/images/someimagename.jpg'.
res.send('/images/'+imageNames[0].name);
);
someJavascript.js
$.get("/pictureItem", function(data, status)
console.log("Found image on url: " + data);
$("#imageId").attr("src", data);
);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your '/pictureItem'
route in Express sends an image.
Your ajax call in your client code seems to be expecting to get an URL back, not an image:
$.get("/pictureItem", function(data, status)
console.log("got image");
$("#imageId").attr("src", data);
);
The usual way you would do this is:
- Set
$("#imageId").attr("src", data);
to an URL that your server knows how to serve the image for. - This will cause the browser to then request that URL from your server.
- When your server gets that image request, it will send the image back.
- The browser will display the image it got back from the server.
I'm not quite sure what the overall problem is you're trying to solve here (you don't show the overall logic of the operation), but you could just do this:
$("#imageId").attr("src", "/pictureItem");
And, then your existing server route would return the desired image when the browser requests the image from the /pictureItem
route.
If I have multiple images, and I want to serve all of them with one request (one get request from server), how can I set the "data" with one string and parse it in server in order to send the right image ?
– user3668129
Nov 9 at 20:40
1
@user3668129 - You don't typically serve multiple images with one request unless you're playing tricks with sprites (which are sub-images buried in one master image). The browser requests an image, the server sends that on image. If the browser wants more than one image, it requests them each separately. Sorry, but I still don't understand what real overall problem you're trying to solve here. Please edit your question to go back a few steps and describe what you're really trying to accomplish here.
– jfriend00
Nov 9 at 20:43
add a comment |
up vote
1
down vote
accepted
Your '/pictureItem'
route in Express sends an image.
Your ajax call in your client code seems to be expecting to get an URL back, not an image:
$.get("/pictureItem", function(data, status)
console.log("got image");
$("#imageId").attr("src", data);
);
The usual way you would do this is:
- Set
$("#imageId").attr("src", data);
to an URL that your server knows how to serve the image for. - This will cause the browser to then request that URL from your server.
- When your server gets that image request, it will send the image back.
- The browser will display the image it got back from the server.
I'm not quite sure what the overall problem is you're trying to solve here (you don't show the overall logic of the operation), but you could just do this:
$("#imageId").attr("src", "/pictureItem");
And, then your existing server route would return the desired image when the browser requests the image from the /pictureItem
route.
If I have multiple images, and I want to serve all of them with one request (one get request from server), how can I set the "data" with one string and parse it in server in order to send the right image ?
– user3668129
Nov 9 at 20:40
1
@user3668129 - You don't typically serve multiple images with one request unless you're playing tricks with sprites (which are sub-images buried in one master image). The browser requests an image, the server sends that on image. If the browser wants more than one image, it requests them each separately. Sorry, but I still don't understand what real overall problem you're trying to solve here. Please edit your question to go back a few steps and describe what you're really trying to accomplish here.
– jfriend00
Nov 9 at 20:43
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your '/pictureItem'
route in Express sends an image.
Your ajax call in your client code seems to be expecting to get an URL back, not an image:
$.get("/pictureItem", function(data, status)
console.log("got image");
$("#imageId").attr("src", data);
);
The usual way you would do this is:
- Set
$("#imageId").attr("src", data);
to an URL that your server knows how to serve the image for. - This will cause the browser to then request that URL from your server.
- When your server gets that image request, it will send the image back.
- The browser will display the image it got back from the server.
I'm not quite sure what the overall problem is you're trying to solve here (you don't show the overall logic of the operation), but you could just do this:
$("#imageId").attr("src", "/pictureItem");
And, then your existing server route would return the desired image when the browser requests the image from the /pictureItem
route.
Your '/pictureItem'
route in Express sends an image.
Your ajax call in your client code seems to be expecting to get an URL back, not an image:
$.get("/pictureItem", function(data, status)
console.log("got image");
$("#imageId").attr("src", data);
);
The usual way you would do this is:
- Set
$("#imageId").attr("src", data);
to an URL that your server knows how to serve the image for. - This will cause the browser to then request that URL from your server.
- When your server gets that image request, it will send the image back.
- The browser will display the image it got back from the server.
I'm not quite sure what the overall problem is you're trying to solve here (you don't show the overall logic of the operation), but you could just do this:
$("#imageId").attr("src", "/pictureItem");
And, then your existing server route would return the desired image when the browser requests the image from the /pictureItem
route.
answered Nov 9 at 20:35
jfriend00
422k49534581
422k49534581
If I have multiple images, and I want to serve all of them with one request (one get request from server), how can I set the "data" with one string and parse it in server in order to send the right image ?
– user3668129
Nov 9 at 20:40
1
@user3668129 - You don't typically serve multiple images with one request unless you're playing tricks with sprites (which are sub-images buried in one master image). The browser requests an image, the server sends that on image. If the browser wants more than one image, it requests them each separately. Sorry, but I still don't understand what real overall problem you're trying to solve here. Please edit your question to go back a few steps and describe what you're really trying to accomplish here.
– jfriend00
Nov 9 at 20:43
add a comment |
If I have multiple images, and I want to serve all of them with one request (one get request from server), how can I set the "data" with one string and parse it in server in order to send the right image ?
– user3668129
Nov 9 at 20:40
1
@user3668129 - You don't typically serve multiple images with one request unless you're playing tricks with sprites (which are sub-images buried in one master image). The browser requests an image, the server sends that on image. If the browser wants more than one image, it requests them each separately. Sorry, but I still don't understand what real overall problem you're trying to solve here. Please edit your question to go back a few steps and describe what you're really trying to accomplish here.
– jfriend00
Nov 9 at 20:43
If I have multiple images, and I want to serve all of them with one request (one get request from server), how can I set the "data" with one string and parse it in server in order to send the right image ?
– user3668129
Nov 9 at 20:40
If I have multiple images, and I want to serve all of them with one request (one get request from server), how can I set the "data" with one string and parse it in server in order to send the right image ?
– user3668129
Nov 9 at 20:40
1
1
@user3668129 - You don't typically serve multiple images with one request unless you're playing tricks with sprites (which are sub-images buried in one master image). The browser requests an image, the server sends that on image. If the browser wants more than one image, it requests them each separately. Sorry, but I still don't understand what real overall problem you're trying to solve here. Please edit your question to go back a few steps and describe what you're really trying to accomplish here.
– jfriend00
Nov 9 at 20:43
@user3668129 - You don't typically serve multiple images with one request unless you're playing tricks with sprites (which are sub-images buried in one master image). The browser requests an image, the server sends that on image. If the browser wants more than one image, it requests them each separately. Sorry, but I still don't understand what real overall problem you're trying to solve here. Please edit your question to go back a few steps and describe what you're really trying to accomplish here.
– jfriend00
Nov 9 at 20:43
add a comment |
up vote
-1
down vote
Have you defined the middleware for static files? If yes you could define a GET
method which retrieves the path to a specific image and return that so you can attach it to the src
attribute.
Your pictureItem
endpoint is return the image file itself. Whereas your src attribute expects a string so it can fetch the image itself.
server.js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended : true));
app.use('/images', express.static(__dirname + '/Images'));
app.get('/pictureItem', function (req, res)
// returns a string as '/images/someimagename.jpg'.
res.send('/images/'+imageNames[0].name);
);
someJavascript.js
$.get("/pictureItem", function(data, status)
console.log("Found image on url: " + data);
$("#imageId").attr("src", data);
);
add a comment |
up vote
-1
down vote
Have you defined the middleware for static files? If yes you could define a GET
method which retrieves the path to a specific image and return that so you can attach it to the src
attribute.
Your pictureItem
endpoint is return the image file itself. Whereas your src attribute expects a string so it can fetch the image itself.
server.js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended : true));
app.use('/images', express.static(__dirname + '/Images'));
app.get('/pictureItem', function (req, res)
// returns a string as '/images/someimagename.jpg'.
res.send('/images/'+imageNames[0].name);
);
someJavascript.js
$.get("/pictureItem", function(data, status)
console.log("Found image on url: " + data);
$("#imageId").attr("src", data);
);
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Have you defined the middleware for static files? If yes you could define a GET
method which retrieves the path to a specific image and return that so you can attach it to the src
attribute.
Your pictureItem
endpoint is return the image file itself. Whereas your src attribute expects a string so it can fetch the image itself.
server.js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended : true));
app.use('/images', express.static(__dirname + '/Images'));
app.get('/pictureItem', function (req, res)
// returns a string as '/images/someimagename.jpg'.
res.send('/images/'+imageNames[0].name);
);
someJavascript.js
$.get("/pictureItem", function(data, status)
console.log("Found image on url: " + data);
$("#imageId").attr("src", data);
);
Have you defined the middleware for static files? If yes you could define a GET
method which retrieves the path to a specific image and return that so you can attach it to the src
attribute.
Your pictureItem
endpoint is return the image file itself. Whereas your src attribute expects a string so it can fetch the image itself.
server.js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended : true));
app.use('/images', express.static(__dirname + '/Images'));
app.get('/pictureItem', function (req, res)
// returns a string as '/images/someimagename.jpg'.
res.send('/images/'+imageNames[0].name);
);
someJavascript.js
$.get("/pictureItem", function(data, status)
console.log("Found image on url: " + data);
$("#imageId").attr("src", data);
);
edited Nov 9 at 20:45
answered Nov 9 at 20:23
Baklap4
1,33811235
1,33811235
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232655%2fwhy-the-image-is-not-shown%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
What is the value of
data
that is being returned? It almost looks like you are returning the byte data of the file itself– Taplar
Nov 9 at 20:09
What is the file that you're sending on request? Is it an
.html
file? Could you specify where your front-endjs
code is located– zfrisch
Nov 9 at 20:13
If, as Taplar surmises you are sending the byte data of the image, you're doing it wrong! All you need to display the image is to update the dom with the new image metadata, and the browser will fetch & render the image itself. Even if you did want the byte data, say to paint into a html5 canvas, it does not look like your client side 'get' request is actually asking for a file. There is no indication that it populates the 'imageNames' parameter. And, in turn, it looks like the imageNames parameter is expected in the body, which is not standard procedure for a GET request!
– bluejack
Nov 9 at 20:26