AJAX and ashx handlers

G

George

I am doing an AJAX call using JQuery on my page to [WebMethod] which returns
JSON objects and everything works fine.
Now I decided to use ashx handler instead of [WebMethod] and simply write
JSON out. Then my problems begun.

So here is JQuery call

$.ajax({
type: 'POST',
url: url,
data: '{id:5}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(result) { ...},
error: function() {... }
});

If url = "Default.aspx/Remove" wich is WebMethod it works perfectly
If url = "ServiceCalls.ashx/Fetch" then the call fails with an error 12030
which is comes from IE's XMLHttpRequest object and stand for
"The connection with the server has been reset or terminated"


Here are strange things.
1. It works in FireFox. Only IE has a problem
2. If i try to debug application it works.
3. If i have a Fiddler running it works.

I tried to analyze with Fiddler 2 requests (and replies) to
"Default.aspx/Remove" and to "ServiceCalls.ashx/Fetch" and they look
absolutely identical.

I rulled out that i output incorrect JSON since it works with Fiddler in IE
and with FireFox.

So I am completely puzzled. What does [WebMethod] does differently?



Thanks
George.
 
G

George

Finally figured it out....

The difference between [WebMethod] and my .ashx hanler is that [WebMethod]
reads the input stream completelly and my .ashx handler did not.

That made IE's XMLHttpRequest object to fail properly get the response. Not
sure why is it being such a "girl". Cause FireFox does not have a problem.

Also it explains why it worked with Fiddler. The Fiddler were reading the
input stream So here is the code the handler must have if you using it in
AJAX call

System.IO.Stream st = context.Request.InputStream;
byte []buf = new byte[100];
while (true)
{
int iRead = st.Read(buf, 0, 100);
if( iRead == 0 )
break;
}
st.Close();

So always read the the InputStream completely even if you are not interested
in it...


George.
 
B

bruce barker

ajax libraries hosted in IE use ActiveXObject("Msxml2.XMLHTTP"), which is
part of the msxml com library. it has been available for years (IE 5.0).

for all other browsers the ajax libraries use the browser builtin object
XMLHttpRequest. the w3c has defined how this object works (optimized for
javascript). its close but not same as the ms com object.

-- bruce (sqlwork.com)


George said:
Finally figured it out....

The difference between [WebMethod] and my .ashx hanler is that [WebMethod]
reads the input stream completelly and my .ashx handler did not.

That made IE's XMLHttpRequest object to fail properly get the response. Not
sure why is it being such a "girl". Cause FireFox does not have a problem.

Also it explains why it worked with Fiddler. The Fiddler were reading the
input stream So here is the code the handler must have if you using it in
AJAX call

System.IO.Stream st = context.Request.InputStream;
byte []buf = new byte[100];
while (true)
{
int iRead = st.Read(buf, 0, 100);
if( iRead == 0 )
break;
}
st.Close();

So always read the the InputStream completely even if you are not interested
in it...


George.



George said:
I am doing an AJAX call using JQuery on my page to [WebMethod] which
returns JSON objects and everything works fine.
Now I decided to use ashx handler instead of [WebMethod] and simply write
JSON out. Then my problems begun.

So here is JQuery call

$.ajax({
type: 'POST',
url: url,
data: '{id:5}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(result) { ...},
error: function() {... }
});

If url = "Default.aspx/Remove" wich is WebMethod it works perfectly
If url = "ServiceCalls.ashx/Fetch" then the call fails with an error 12030
which is comes from IE's XMLHttpRequest object and stand for
"The connection with the server has been reset or terminated"


Here are strange things.
1. It works in FireFox. Only IE has a problem
2. If i try to debug application it works.
3. If i have a Fiddler running it works.

I tried to analyze with Fiddler 2 requests (and replies) to
"Default.aspx/Remove" and to "ServiceCalls.ashx/Fetch" and they look
absolutely identical.

I rulled out that i output incorrect JSON since it works with Fiddler in
IE and with FireFox.

So I am completely puzzled. What does [WebMethod] does differently?



Thanks
George.
 
G

George

Starting from IE 7 there is builtin XMLHttpRequest object (in IE)
http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx

Nevertheless my post was not about that....


George.

bruce barker said:
ajax libraries hosted in IE use ActiveXObject("Msxml2.XMLHTTP"), which is
part of the msxml com library. it has been available for years (IE 5.0).

for all other browsers the ajax libraries use the browser builtin object
XMLHttpRequest. the w3c has defined how this object works (optimized for
javascript). its close but not same as the ms com object.

-- bruce (sqlwork.com)


George said:
Finally figured it out....

The difference between [WebMethod] and my .ashx hanler is that
[WebMethod]
reads the input stream completelly and my .ashx handler did not.

That made IE's XMLHttpRequest object to fail properly get the response.
Not
sure why is it being such a "girl". Cause FireFox does not have a
problem.

Also it explains why it worked with Fiddler. The Fiddler were reading the
input stream So here is the code the handler must have if you using it in
AJAX call

System.IO.Stream st = context.Request.InputStream;
byte []buf = new byte[100];
while (true)
{
int iRead = st.Read(buf, 0, 100);
if( iRead == 0 )
break;
}
st.Close();

So always read the the InputStream completely even if you are not
interested
in it...


George.



George said:
I am doing an AJAX call using JQuery on my page to [WebMethod] which
returns JSON objects and everything works fine.
Now I decided to use ashx handler instead of [WebMethod] and simply
write
JSON out. Then my problems begun.

So here is JQuery call

$.ajax({
type: 'POST',
url: url,
data: '{id:5}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(result) { ...},
error: function() {... }
});

If url = "Default.aspx/Remove" wich is WebMethod it works perfectly
If url = "ServiceCalls.ashx/Fetch" then the call fails with an error
12030
which is comes from IE's XMLHttpRequest object and stand for
"The connection with the server has been reset or terminated"


Here are strange things.
1. It works in FireFox. Only IE has a problem
2. If i try to debug application it works.
3. If i have a Fiddler running it works.

I tried to analyze with Fiddler 2 requests (and replies) to
"Default.aspx/Remove" and to "ServiceCalls.ashx/Fetch" and they look
absolutely identical.

I rulled out that i output incorrect JSON since it works with Fiddler
in
IE and with FireFox.

So I am completely puzzled. What does [WebMethod] does differently?



Thanks
George.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top