ASP.NET JQuery AJAX POST returns data but within a 401 response

对着背影说爱祢 提交于 2019-12-22 14:06:45

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9908707/asp-net-jquery-ajax-post-returns-data-but-within-a-401-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!