By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,856 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,856 IT Pros & Developers. It's quick & easy.

"AJAX" - sending but don't need response?

P: n/a
I have a situation where I want to send data, but I have no need for a
response. It seems to me that XMLHTTPRequest is the best way to send the
data, but I don't need any response back from the server.

Basically, I'm writing js errors to an error log on the server side -
and there is no need to inform the user that the error has been logged.

The problem is that I don't want to sit with the request open & waiting
for a response, when I have no need for the response. I suppose I could
have the server respond with just "OK" or something like that, but it's
really not relevant

I was thinking of just aborting the request on readystate=1 (at that
point, the server-side script should have executed already, right?), but
I was wondering if there were any other methods to JUST send..

Thanx
Mar 6 '06 #1
Share this Question
Share on Google+
20 Replies


P: n/a
VK

Tony wrote:
I have a situation where I want to send data, but I have no need for a
response. It seems to me that XMLHTTPRequest is the best way to send the
data, but I don't need any response back from the server.


Not a script question. See HTTP specs, 204 / No Content response

Mar 6 '06 #2

P: n/a
Tony said the following on 3/6/2006 3:33 PM:
I have a situation where I want to send data, but I have no need for a
response. It seems to me that XMLHTTPRequest is the best way to send the
data, but I don't need any response back from the server.


No, XMPHTTPRequest is not the best way to send the data. Especially
since you don't need the response.

myVar = new Image();
myVar.src = "pathToServerSideScript.ext?" + errorMessage;

Then the server side script executes and nothing is done with the response.

This is so widely asked that its in the FAQ:

<URL: http://jibbering.com/faq/#FAQ4_34 >

"How do I execute a server side script?"

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 6 '06 #3

P: n/a
Randy Webb wrote:
Tony said the following on 3/6/2006 3:33 PM:
I have a situation where I want to send data, but I have no need for a
response. It seems to me that XMLHTTPRequest is the best way to send
the data, but I don't need any response back from the server.


No, XMPHTTPRequest is not the best way to send the data. Especially
since you don't need the response.

myVar = new Image();
myVar.src = "pathToServerSideScript.ext?" + errorMessage;


This old trick always work, but it's just a trick, I think the XHR is
the right way of doing it. Besides the fact that the query string has a
size limit too, so this trick isn't the best way to send data either.

If you don't need to send a good amount of data or if you need
compatibility with old browsers, ok... Use the query string.

Otherwise I'm all for XHR \o/, since for me it's clearly the right way,
if the response doesn't matter for you, don't even generate it! Since
you will just need to check headers (there's a method to just fetch the
headers on this XHR object, but I think it's not present on IE, search a
little hehe).
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 6 '06 #4

P: n/a
Randy Webb wrote:
Tony said the following on 3/6/2006 3:33 PM:
I have a situation where I want to send data, but I have no need for a
response. It seems to me that XMLHTTPRequest is the best way to send
the data, but I don't need any response back from the server.

No, XMPHTTPRequest is not the best way to send the data. Especially
since you don't need the response.

myVar = new Image();
myVar.src = "pathToServerSideScript.ext?" + errorMessage;

Then the server side script executes and nothing is done with the response.

This is so widely asked that its in the FAQ:

<URL: http://jibbering.com/faq/#FAQ4_34 >

"How do I execute a server side script?"


Ah - I DID search (although I didn't think to look specifically at the
FAQ for this group) - but didn't come up with much. Probably wasn't
phrasing it in the best way, I'd say.

Thanx
Mar 6 '06 #5

P: n/a
VK wrote:
Tony wrote:
I have a situation where I want to send data, but I have no need for a
response. It seems to me that XMLHTTPRequest is the best way to send the
data, but I don't need any response back from the server.

Not a script question. See HTTP specs, 204 / No Content response


Not quite what I'm looking for - in that case, there is STILL a
response. Unless I'm badly misunderstanding something, that would
generate the full set of readyState changes from 1-4, right?

What I'd like is to not even have to get a response back AT ALL, not
even a 204, no headers, nothing...

Mar 6 '06 #6

P: n/a
VK

Tony wrote:
VK wrote:
Tony wrote:
I have a situation where I want to send data, but I have no need for a
response. It seems to me that XMLHTTPRequest is the best way to send the
data, but I don't need any response back from the server.

Not a script question. See HTTP specs, 204 / No Content response


Not quite what I'm looking for - in that case, there is STILL a
response. Unless I'm badly misunderstanding something, that would
generate the full set of readyState changes from 1-4, right?

What I'd like is to not even have to get a response back AT ALL, not
even a 204, no headers, nothing...


Right - and for this you don't need any Ajax nor even script. Simply
submit your form using GET or POST. You server has to send back 204 No
Content. For user it will look like nothing changed at all - browser
will stay on the same page, no refresh.

Mar 6 '06 #7

P: n/a
Tony wrote:
Randy Webb wrote:
Ah - I DID search (although I didn't think to look specifically at the
FAQ for this group) - but didn't come up with much. Probably wasn't
phrasing it in the best way, I'd say.


Man, why are you so crazy to avoid responses??? You're looking like
those people that say:

PLEASE, HELP ME!!! I want to do "X", but not this way, not using "Y" and
don't even think about "Z"!

If you don't need answer, just don't output anything, the responses will
always be there, unless the browser cuts the connection \o/

Even this trick will try to read some content... It's expecting a binary
representation of an image, but it will receive an strange header and
won't parse =)
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 6 '06 #8

P: n/a
On 06/03/2006 22:39, Jonas Raoni wrote:

[snip]
Man, why are you so crazy to avoid responses???
Indeed. HTTP is a request/response transport protocol. Any request will
illicit a response unless something badly wrong happens.

[snip]
Even this trick [modifying an image URL] will try to read some
content... It's expecting a binary representation of an image, but it
will receive an strange header and won't parse =)


Not true. The image request will still be a HTTP request, so the browser
will be expecting headers. It would only be parsing strange information
if the server returned content (with a 200 OK response, for example) and
that data wasn't image data in the format indicated by the Content-Type
response header.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Mar 6 '06 #9

P: n/a
Michael Winter wrote:
On 06/03/2006 22:39, Jonas Raoni wrote:
Not true. The image request will still be a HTTP request, so the browser
will be expecting headers. It would only be parsing strange information
if the server returned content (with a 200 OK response, for example) and
that data wasn't image data in the format indicated by the Content-Type
response header.


Yes, that's right, besides these things you said, it would be smart to
check the image header. But this isn't a rule, it depends of the browser =]

If the browser has a bug on the image parser, I could send a recognized
image content-type, send the right image header and then output a
content to generate a crazy loop on it, for sure I would need to know
how it works.
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 6 '06 #10

P: n/a
VK wrote:
Tony wrote:
Not quite what I'm looking for - in that case, there is STILL a
response. Unless I'm badly misunderstanding something, that would
generate the full set of readyState changes from 1-4, right?

What I'd like is to not even have to get a response back AT ALL, not
even a 204, no headers, nothing...

Right - and for this you don't need any Ajax nor even script. Simply
submit your form using GET or POST. You server has to send back 204 No
Content. For user it will look like nothing changed at all - browser
will stay on the same page, no refresh.


OK - that helps some. I actually think that it won't work in the
specific circumstance I have, but I will certainly check it out. Thanx

Mar 6 '06 #11

P: n/a
Jonas Raoni wrote:

Man, why are you so crazy to avoid responses??? You're looking like
those people that say:

PLEASE, HELP ME!!! I want to do "X", but not this way, not using "Y" and
don't even think about "Z"!
I guess I am, at that :) Well, sometimes, that's just the way the
problem gets posed to me, you know?
If you don't need answer, just don't output anything, the responses will
always be there, unless the browser cuts the connection \o/
Which is my main concern - I don't want to open a request and have it
sit there open indefinitely. Also, I have a need to save as much
bandwidth as possible for this (which no response would work for). I
suppose I could abort it on a short timeout or do something like that -
I'm working through the possibilities...

Mainly, I'm just looking for some input: A lot of times, even if I don't
get a direct answer from posting here, the feedback gets me pointed in a
better direction...
Even this trick will try to read some content... It's expecting a binary
representation of an image, but it will receive an strange header and
won't parse =)

Mar 6 '06 #12

P: n/a
Tony wrote:
VK wrote:
Tony wrote:
I have a situation where I want to send data, but I have no need for a
response. It seems to me that XMLHTTPRequest is the best way to send the
data, but I don't need any response back from the server.
Not a script question. See HTTP specs, 204 / No Content response


Not quite what I'm looking for - in that case, there is STILL a
response.


There is _always_ a response, unless the HTTP server is down. However,
status code 204 indicates that the response body can be disregarded by
the client.
Unless I'm badly misunderstanding something, that would
generate the full set of readyState changes from 1-4, right?
Most certainly. You could just test it.
What I'd like is to not even have to get a response back AT ALL, not
even a 204, no headers, nothing...


Do not use HTTP then. Ref. RFC1945/2616: HTTP is a request-response chain.
PointedEars
Mar 7 '06 #13

P: n/a
Jonas Raoni said the following on 3/6/2006 5:19 PM:
Randy Webb wrote:
Tony said the following on 3/6/2006 3:33 PM:
I have a situation where I want to send data, but I have no need for
a response. It seems to me that XMLHTTPRequest is the best way to
send the data, but I don't need any response back from the server.
No, XMPHTTPRequest is not the best way to send the data. Especially
since you don't need the response.

myVar = new Image();
myVar.src = "pathToServerSideScript.ext?" + errorMessage;


This old trick always work,


Remember that! :)
but it's just a trick, I think the XHR is the right way of doing it.
Then you think wrong :)
Besides the fact that the query string has a size limit too, so this
trick isn't the best way to send data either.
Yes, the querystring has a size limit. But using the img.src "trick" as
you call it is more reliable and can be used and get around the URL size
limit. Create 10 images and now you have 10 times the data going to the
server - as long as the server knows how to accept/handle it.

If you don't need to send a good amount of data or if you need
compatibility with old browsers, ok... Use the query string.
Or if you just want a simple way where you don't need the results (which
is precisely what the OP wanted).
Otherwise I'm all for XHR \o/, since for me it's clearly the right way,
if the response doesn't matter for you, don't even generate it! Since
you will just need to check headers (there's a method to just fetch the
headers on this XHR object, but I think it's not present on IE, search a
little hehe).


To check the headers, guess what? The response has to be generated or
there is no header to check. And that is not what the OP wanted - no
response.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 7 '06 #14

P: n/a
Jonas Raoni said the following on 3/6/2006 5:39 PM:
Tony wrote:
Randy Webb wrote:
Ah - I DID search (although I didn't think to look specifically at the
FAQ for this group) - but didn't come up with much. Probably wasn't
phrasing it in the best way, I'd say.


Man, why are you so crazy to avoid responses??? You're looking like
those people that say:

PLEASE, HELP ME!!! I want to do "X", but not this way, not using "Y" and
don't even think about "Z"!


Where did he say "don't even think about Z"? He didn't. He asked a
question and got a better reply. One that is even covered in this groups
FAQ.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 7 '06 #15

P: n/a
Randy Webb wrote:
Jonas Raoni said the following on 3/6/2006 5:19 PM:
Randy Webb wrote:
Tony said the following on 3/6/2006 3:33 PM: This old trick always work,


Remember that! :)
but it's just a trick, I think the XHR is the right way of doing it.


Then you think wrong :)


It just depends of your point of view, the right solution is the
solution that fills the requirements.
Besides the fact that the query string has a size limit too, so this
trick isn't the best way to send data either.


Yes, the querystring has a size limit. But using the img.src "trick" as
you call it is more reliable and can be used and get around the URL size
limit. Create 10 images and now you have 10 times the data going to the
server - as long as the server knows how to accept/handle it.


This sounds bizarre to me, a workaround over another workaround. If
there's a way to avoid making such strange things, why not use it? :]

You'll end up having something like this:

(new Image).src = "add?id=1&piece=0&data=abc";
(new Image).src = "add?id=1&piece=1&data=def";
:

If you don't need to send a good amount of data or if you need
compatibility with old browsers, ok... Use the query string.


Or if you just want a simple way where you don't need the results (which
is precisely what the OP wanted).


For me it will always look like what we call here "gambiarra". Yes! It's
very simple, but it's a subversion of the feature, the Image is supposed
to load images, not make fancy requests.
And that is not what the OP wanted - no response.


He's lost.

"Basically, I'm writing js errors to an error log on the server side -
and there is no need to inform the user that the error has been logged."

He doesn't need to inform the user, but it would be nice to be sure that
the error was logged right ;]
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 7 '06 #16

P: n/a
Randy Webb wrote:
Jonas Raoni said the following on 3/6/2006 5:39 PM:
Man, why are you so crazy to avoid responses??? You're looking like
those people that say:

PLEASE, HELP ME!!! I want to do "X", but not this way, not using "Y"
and don't even think about "Z"!


Where did he say "don't even think about Z"? He didn't. He asked a
question and got a better reply. One that is even covered in this groups
FAQ.


"You're *looking like* those people that say [...]"
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 7 '06 #17

P: n/a
Jonas Raoni said the following on 3/7/2006 2:18 AM:
Randy Webb wrote:
Jonas Raoni said the following on 3/6/2006 5:19 PM:
Randy Webb wrote:

Tony said the following on 3/6/2006 3:33 PM:
This old trick always work,


Remember that! :)
but it's just a trick, I think the XHR is the right way of doing it.


Then you think wrong :)


It just depends of your point of view, the right solution is the
solution that fills the requirements.


Very true but the right solution is also a solution that doesn't
overfill the requirements. A sledgehammer will drive a nail in your wall
to hang a picture but that isn't the best solution :)
Besides the fact that the query string has a size limit too, so this
trick isn't the best way to send data either.


Yes, the querystring has a size limit. But using the img.src "trick"
as you call it is more reliable and can be used and get around the URL
size limit. Create 10 images and now you have 10 times the data going
to the server - as long as the server knows how to accept/handle it.


This sounds bizarre to me, a workaround over another workaround. If
there's a way to avoid making such strange things, why not use it? :]

You'll end up having something like this:

(new Image).src = "add?id=1&piece=0&data=abc";
(new Image).src = "add?id=1&piece=1&data=def";
:


I didn't say it wouldn't be messy :) And yes, trying to get around the
URL limit would be messy with an Image src.

If the URL length is a problem, then you can submit a hidden IFrame to
itself. The response gets handled by the browser but the user still
never sees it.

But, I have never seen an error message that exceeded 1000 characters,
not even a custom error message. But if the custom error message is that
large, then it should be constructed on the server anyway.

If you don't need to send a good amount of data or if you need
compatibility with old browsers, ok... Use the query string.


Or if you just want a simple way where you don't need the results
(which is precisely what the OP wanted).


For me it will always look like what we call here "gambiarra". Yes! It's
very simple, but it's a subversion of the feature, the Image is supposed
to load images, not make fancy requests.


It's not about subversion of a feature as much as it is supported
features though. When the XMLHTTPRequest gains wider support, then yes
it would be the best way. But right now, it just doesn't have wide
enough support whereas the image.src route does.
And that is not what the OP wanted - no response.


He's lost.


And you want to tell him to use an XMLHTTPRequest object over setting
the src of a dummy Image()? Think about that one :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 7 '06 #18

P: n/a
Randy Webb wrote:
It just depends of your point of view, the right solution is the
solution that fills the requirements.
Very true but the right solution is also a solution that doesn't
overfill the requirements. A sledgehammer will drive a nail in your wall
to hang a picture but that isn't the best solution :)


I agree. But I do like strong solutions, whenever I make little fixes, I
always find myself making new little fixes, because they got outdated
haha ;]
If the URL length is a problem, then you can submit a hidden IFrame to
itself. The response gets handled by the browser but the user still
never sees it.
As I don't work as web developer, I stopped writing these things when I
saw they were quite strange solutions. Now I just aim at updated
versions of ie, ff and opera, the rest is rest (I would test on safari,
but I don't have a mac haha), I try to do things in a not blocking way,
so if the user insists about using bad gasoline on his Ferrari, there's
nothing I can do :]
But, I have never seen an error message that exceeded 1000 characters,
not even a custom error message. But if the custom error message is that
large, then it should be constructed on the server anyway.
I doubt he will exceed this limit and I'm not sure if it's just 1000
chars, I tested once and I received a lot more than that ;]
It's not about subversion of a feature as much as it is supported
features though. When the XMLHTTPRequest gains wider support, then yes
it would be the best way. But right now, it just doesn't have wide
enough support whereas the image.src route does.
As I said, old browsers doesn't matter for me =]
And you want to tell him to use an XMLHTTPRequest object over setting
the src of a dummy Image()? Think about that one :)


No, I just said what I would do, he has the right to decide what he will
use... Even this XHR isn't a nice way since it's not a standard, but it
doesn't matter :]
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 7 '06 #19

P: n/a
"Tony" <to****@dslextreme.WHATISTHIS.com> wrote in message
news:12*************@corp.supernews.com...
I have a situation where I want to send data, but I have no need for a
response. It seems to me that XMLHTTPRequest is the best way to send the
data, but I don't need any response back from the server.

Basically, I'm writing js errors to an error log on the server side - and
there is no need to inform the user that the error has been logged.

The problem is that I don't want to sit with the request open & waiting
for a response, when I have no need for the response. I suppose I could
have the server respond with just "OK" or something like that, but it's
really not relevant

I was thinking of just aborting the request on readystate=1 (at that
point, the server-side script should have executed already, right?), but I
was wondering if there were any other methods to JUST send..

Thanx


I've coded some AJAX where I didn't know a response. Since I didn't
need it to do anything when it was finished, I just didn't declared an
onreadystatechange function. So basically, all you're doing is sending
the data and that's it.
Mar 20 '06 #20

P: n/a
VK

Tony wrote:
Which is my main concern - I don't want to open a request and have it
sit there open indefinitely. Also, I have a need to save as much
bandwidth as possible for this (which no response would work for). I
suppose I could abort it on a short timeout or do something like that -
I'm working through the possibilities...


You are just thinking too linear: a common error reproduced in many
server-side scripts ;-)
1) server got request
2) server is working with request
3) server sent response back

If the stage 2 takes too long, people starting to bother with problems
similar to yours. A hint: stages 2 and 3 are _interchangeable_

1) server got request
2) server sent back 204 No Content to close connection
3) server is working with request

Your traffic and connection time by definition cannot be lesser than
necessary to send data from client to server, so this is a maximum
effective schema one can have. I guess you can go with 1 (one) extra
TCP/IP package sent from the server ;-)

Mar 21 '06 #21

This discussion thread is closed

Replies have been disabled for this discussion.