2

I have a Web page within my application that needs to call a web service I have set up to return a list of objects. This call is set up like so

$(document).ready(function() {
    var response = $.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        url: '/Ajax/service.asmx/GetObjects'
    });

    response.success(function(data) {
       $('#bodyText').html(data);
    });

    response.complete(function() {
       alert('ajax complete');
    });

});

My service looks like this

namespace AjaxTest.Ajax
{
    #region
using System.ComponentModel;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

#endregion

/// <summary>
/// Summary for boat
/// </summary>
[WebService(Namespace = "http://quality/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Boat : WebService
{
    #region Public Methods and Operators

    /// <summary>
    /// The Get Models method.
    /// </summary>
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void GetModels()
    {
        var models = Com.somedll.GetModels();
        var serializer = new JavaScriptSerializer();
        var response = models.Count != 0
                ? serializer.Serialize(models)
                : serializer.Serialize(new Error { Code = "500", Message = "Model Retrieval Failed" });
        this.Context.Response.Clear();
        this.Context.Response.ContentType = "application/json";
        this.Context.Response.Flush();
        this.Context.Response.Write(response);
    }

    #endregion
    }
}

I am getting the following errors Server cannot append header after HTTP headers have been sent. and looking at Firebug the request seems to be being sent multiple times at least 4 for each request, can anyone help. Please let me know if you need any more information

2
  • 2
    Flush should be the last thing? try swapping order of: this.Context.Response.Flush(); this.Context.Response.Write(response); Commented Mar 28, 2012 at 13:40
  • @BobTodd still getting the same errors Server cannot append header after HTTP headers have been sent. really weird thanks for the correction guys Commented Mar 28, 2012 at 13:50

2 Answers 2

2

Move your Response.Write call before your call to Flush.

    this.Context.Response.Write(response);
    this.Context.Response.Flush();

UPDATE: You can also try removing the ContentType setter, since you're already stating that your response type is JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

I have removed the content type statement this has had no effect, I am struggling to see what is wrong, the page looks fine the response (with your help) now looks fine. But I still get the errors?
1

Can you try stripping it back from:

this.Context.Response.Clear();
        this.Context.Response.ContentType = "application/json";
        this.Context.Response.Flush();
        this.Context.Response.Write(response);

to just:

this.Context.Response.Write(response);

or even

this.Context.Response.BufferOutput = true;
this.Context.Response.Write(response);
this.Context.Response.End();

edit

It sounds like you could be experiencing this behaviour:

  1. you write something to repsonse
  2. you flush
  3. your code triggers an error, asp.net tries to rewrite the headers, which is disallowed because the call to Flush() already wrote the headers

stick a try catch in there and see if anything throws.

1 Comment

I have tried this and this has confirmed that there is no error present within my web service one tick in the box :). this lead me to examine the javascript again and when I remove the contentType from this I get data back recognised as JSON, but I am still getting the recursion shown in FireBug I am not sure why but now the response has changed to a 401 unauthorized to all 6 calls. Just to reiterate I am only calling the service once

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.