1

I'd like to be able to set an image source from a server but also have it return extra data relating to another server-side action.

I'll try to explain.

In my index.htm file I have the following in the <body> ...

<div id="BasicDemo" >
    <img id="nscreen" />
</div>
<script type="text/javascript">setImageSource()</script>

The setImageSource() simply does this...

$("#nscreen").attr("src", "/control?size=800x480");

I'm not an experienced HTML coder but I'm experienced enough at coding in general to understand that the above is an implicit HTTP GET request against server-side code in order to request an image which is used to set the <img> source.

What I'd like to do in psuedo-code is...

var someResponse = getImageAndExtra("/control?size=800x480");
$("#nscreen").attr("src", someResponse.imagePath);
var extraData = someResponse.extra;

I'm not directly in control of the server-side code but I know the developer and will be working on this.

What sort of call can I use to request both an image source as well as extra data and still be able to set the "src" attribute? Also at the other end, how would the server-side code construct the response?

I can't help feeling I'm missing something really simple here.

1 Answer 1

1

Your server code would have to return a web page independent of the image.

This web page would contain the path to the actual image, and then (independently), your additional data.

One very common format for this type of communication is JSON. The server constructs a page which looks like this:

{extra: "some-data", image: "/path/to/real/image"}

Just that, no HTML, and it sets the Content-Type of the response to application/x-json instead of text/html.

Then the JS does an XMLHTTPRequest to fetch the above JSON, parses the response, and then sets the src attribute.

There is no location to which you could set the image source which would let you extract additional info (barring image steganography or header tricks or other dirty dealings).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. JSON is something I was looking at but wasn't sure how it would work. Your explanation on Content-Type helps a great deal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.