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 ?



enter image description here










share|improve this question



















  • 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-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














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 ?



enter image description here










share|improve this question



















  • 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-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












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 ?



enter image description here










share|improve this question















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 ?



enter image description here







javascript jquery html node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 20:16

























asked Nov 9 at 20:08









user3668129

87311225




87311225







  • 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-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












  • 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-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







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












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:



  1. Set $("#imageId").attr("src", data); to an URL that your server knows how to serve the image for.

  2. This will cause the browser to then request that URL from your server.

  3. When your server gets that image request, it will send the image back.

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






share|improve this answer




















  • 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

















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);
);





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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:



    1. Set $("#imageId").attr("src", data); to an URL that your server knows how to serve the image for.

    2. This will cause the browser to then request that URL from your server.

    3. When your server gets that image request, it will send the image back.

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






    share|improve this answer




















    • 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














    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:



    1. Set $("#imageId").attr("src", data); to an URL that your server knows how to serve the image for.

    2. This will cause the browser to then request that URL from your server.

    3. When your server gets that image request, it will send the image back.

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






    share|improve this answer




















    • 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












    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:



    1. Set $("#imageId").attr("src", data); to an URL that your server knows how to serve the image for.

    2. This will cause the browser to then request that URL from your server.

    3. When your server gets that image request, it will send the image back.

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






    share|improve this answer












    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:



    1. Set $("#imageId").attr("src", data); to an URL that your server knows how to serve the image for.

    2. This will cause the browser to then request that URL from your server.

    3. When your server gets that image request, it will send the image back.

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







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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
















    • 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












    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);
    );





    share|improve this answer


























      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);
      );





      share|improve this answer
























        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);
        );





        share|improve this answer














        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);
        );






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 20:45

























        answered Nov 9 at 20:23









        Baklap4

        1,33811235




        1,33811235



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo