AJAX and ashx handlers

  • Thread starter Thread starter George
  • Start date Start date
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.
 
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.
 
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.
 
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.
 
Back
Top