Your URL is the culprit. And the way you are passing data.
Doing http://myserver.com/index.gif?id=1&event=e is a GET request - the
params are on URl directly. If you want this data to appear in the POST
then you need to pass them right:
$.ajax({
async: false,
type: 'POST',
url: 'myserver.com/index.gif',
data: 'id=1&event=e',
cache: false,
timeout: 0
});
This *should* take care of the OPTIONS thing (I don't know anymore
details on that at the moment..)
Some observations. Did you really mean to do a synchronous call?
Synchronous calls are "blocking" - they stop further processing until a
result is returned. If that result never returns, you've hung your
code. This is what the timeout attempts to handle. There IS a time and
place for synchronous calls, but with what you've given us so far I
don't think this is one of them. To make it asynchronous (which is what
the first A in Ajax means), you'd change your code like this:
$.ajax({
type: 'POST',
url: 'myserver.com/index.gif',
data: 'id=1&event=e',
cache: false,
});
Next, you are setting cache to false - I'm assuming because you really
want the server to process each request. What I've done in the past is
to just attach a random number to the URL.
url: 'myserver.com/index.gif?r=' + Math.random()
this works well enough for my needs, though I'm not sure if this is a
"best practice".
Next, your url parameter points to 'myserver.com/index.gif'. $.ajax
cannot do cross domain requests (unless you use jsonp, which is a little
beyond this discussion...). The URL should be a relative path to a file
from your current page. Also, why are you requesting index.gif? Do you
have serverside configs that redirect index.gif? Why not just call
"mypage.php" instead? Remember that an Ajax call is a REQUEST of a
resource, and that resource can return whatever. I suspect you are
converting some old school code where the request for a gif file was a
precursor to modern Ajax, but could be wrong.
Finally, Your request will return a response of some type. You should
be handling that response in the success and/or error properties. Then
again, they are not required. But what happens if something unexpected
happens? Does your app fail gracefully?
This should get you pointed in the right direction. I suspect you may
have problems with your event parameter (it appears it should be an
object, not the letter 'e'...), but that is relatively minor.
Hope this helps.
Shawn
Ryan White wrote:
Hey All,
So I am having a weird situation when using the $.ajax method. And have
a couple questions regarding this. What I am doing is basically just
using this AJAX call to send a 'pixel' back to my server so I can
collect stats on events. This event fires once every 30 seconds.
$.ajax({
async: false,
type: 'POST',
url: myserver.com/index.gif?id=1&event=e
<http://myserver.com/index.gif?id=1&event=e>
cache: false,
timeout: 0
});
My requests keep coming in as OPTIONS call, instead of POST. Every once
in a while I will get a random rogue GET request that basically mimics
the previous OPTIONS request. The OPTIONS come very 30 seconds. The
rogue GET comes whenever, only seldomly.
1) What is OPTIONS and why would it come out as OPTIONS instead of POST?
2) Why would GET requests come at all? Why would they come randomly
(seemingly) without being called?
3) Is this a proper way of sending a 'pixel' back to my server to track
stats? If not, what is a better way?
Very new to AJAX, and definitely don't know all the internals. Any help
would be greatly appreciated.
Thanks in advance....