【问题标题】:ASP.NET JQuery AJAX POST returns data but within a 401 responseASP.NET JQuery AJAX POST 返回数据但在 401 响应内
【发布时间】:2012-03-28 13:38:01
【问题描述】:

我的应用程序中有一个网页需要调用我已设置的 Web 服务以返回对象列表。这个调用是这样设置的

$(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');
    });

});

我的服务是这样的

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
    }
}

我收到以下错误服务器无法在发送 HTTP 标头后附加标头。 并且查看 Firebug 请求似乎被多次发送,每个请求至少 4 ,任何人都可以帮忙。如果您需要更多信息,请告诉我

【问题讨论】:

  • 冲洗应该是最后一件事吗?尝试交换顺序:this.Context.Response.Flush(); this.Context.Response.Write(response);
  • @BobTodd 仍然收到相同的错误 服务器无法在发送 HTTP 标头后附加标头。 真的很奇怪,感谢各位纠正

标签: c# jquery ajax asp.net-3.5


【解决方案1】:

将您的 Response.Write 呼叫移至 Flush 之前。

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

更新:您也可以尝试删除 ContentType 设置器,因为您已经声明您的响应类型是 JSON。

【讨论】:

  • 我已经删除了内容类型声明这没有任何效果,我正在努力查看问题所在,页面看起来很好响应(在您的帮助下)现在看起来很好。但我仍然得到错误?
【解决方案2】:

您可以尝试从以下位置剥离它吗:

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

只是:

this.Context.Response.Write(response);

甚至

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

编辑

听起来您可能会遇到这种行为:

  1. 你写一些东西来回应
  2. 你冲洗
  3. 您的代码触发错误,asp.net 尝试重写标头,这是不允许的,因为对 Flush() 的调用已写入标头

在其中插入一个 try catch,看看是否有任何异常。

【讨论】:

  • 我已经尝试过了,这证实了我的网络服务中没有错误出现在方框中的一勾:)。这导致我再次检查 javascript,当我从中删除 contentType 时,我得到的数据被识别为 JSON,但我仍然得到 FireBug 中显示的递归我不知道为什么但现在响应已更改为 401 未授权 到所有 6 个呼叫。只是为了重申我只调用该服务一次
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多