gpt4 book ai didi

c# - ASP.NET JQuery AJAX POST 返回数据但在 401 响应中

转载 作者:行者123 更新时间:2023-11-30 22:30:00 25 4
gpt4 key购买 nike

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

$(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 header 后附加 header 。并查看 Firebug,每个请求似乎多次发送请求至少 4,任何人都可以提供帮助。如果您需要更多信息,请告诉我

最佳答案

Response.Write 调用移到 Flush 之前。

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

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

关于c# - ASP.NET JQuery AJAX POST 返回数据但在 401 响应中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9908707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com