本文介绍了ASP.NET AJAX的JQuery POST返回数据,但401响应中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序需要调用我已经设置了返回对象列表的Web服务中的一个网页。该呼叫建立像这样

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

我收到以下错误服务器的HTTP标头后不能追加头已发送。
和看萤火虫的要求似乎是被多次发送至少 4 为每个请求,谁能帮助。请让我知道如果你需要任何更多的信息

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

推荐答案

移动你的的Response.Write 呼叫之前调用刷新

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

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



更新:您也可以尝试删除的ContentType 二传手,因为你已经说明你的反应类型是JSON。

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

这篇关于ASP.NET AJAX的JQuery POST返回数据,但401响应中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:45