Monday, December 29, 2008

[jQuery] Re: HttpHandler not returning JSON data

I did try and am able to use the json.net class for it. The
HttpHandler is returning json data now. Thanks.

Now I was trying to get 10 items from the json file. My url in
$.getJSON looks like:

$.getJSON("myjsonfile.json?count=10", {}, function(data) {
$.each(data, function(i, item) {
alert(item.title);
alert(i);
});
});

But the above code returns all 20 items in my file. What mistakes am I
making?

On Dec 28, 3:57 pm, MorningZ <morni...@gmail.com> wrote:
> Want to make life MUCH easier than all those IE-specific hacks?
>
> Check out Json.NET
>
> http://james.newtonking.com/pages/json-net.aspx
>
> On Dec 28, 1:15 am, JQueryProgrammer <jain.ashis...@gmail.com> wrote:
>
> > I got the solution to this. Actually this code was working in Firefox
> > and not in IE. When I tried to run in IE, it was giving error 12030
> > (Server unexpectedly terminated connection) from IE's XMLHttpRequest
> > object. The solution:
>
> > In my HttpHandler I included the code:
>
> > 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();
>
> > Actually ASp.Net does not read the complete incoming input stream
> > automatically. In order for IE's XMLHttpRequest object to properly
> > read return stream the input stream must be completely read by the
> > server (even if you are not interested in it).
>
> > On Dec 28, 10:29 am, JQueryProgrammer <jain.ashis...@gmail.com> wrote:
>
> > > Hi All,
>
> > > I was working with a basic example where I wanted to get JSON data
> > > from the HttpHandler. My javascript code looks like:
>
> > > $.ajax({
> > >     url: 'Handler1.ashx',
> > >     type: 'POST',
> > >     data: { name: 'MyName' },
> > >     contextType: 'application/json; charset=utf-8',
> > >     dataType: 'json',
> > >     success: function(data) {
> > >         alert("Data Saved " + data.name);
> > >     }
>
> > > });
>
> > > My Handler1.ashx code looks like:
>
> > > context.Response.ContentType = "application/json";
> > > string results = "{ 'name': '"+ context.Request.Params["name"] +"' }";
> > > context.Response.Write(results);
>
> > > But I am not getting the results right. Infact I am not getting any
> > > result in data.name. Please let me know what is the mistake and what
> > > is the correct way to get JSON data.

No comments: