C# ASP.NET JQuery AJAX POST返回数据,但在401响应内
我的应用程序中有一个网页,需要调用我设置的Web服务以返回对象列表。这个电话是这样安排的C# ASP.NET JQuery AJAX POST返回数据,但在401响应内,c#,jquery,ajax,asp.net-3.5,C#,Jquery,Ajax,Asp.net 3.5,我的应用程序中有一个网页,需要调用我设置的Web服务以返回对象列表。这个电话是这样安排的 $(document).ready(function() { var response = $.ajax({ type: 'POST', contentType: 'application/json; charset=utf-8', url: '/Ajax/service.asmx/GetObjects' }); response.s
$(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
}
}
namespace AjaxTest.Ajax
{
#区域
使用系统组件模型;
使用System.Web.Script.Serialization;
使用System.Web.Script.Services;
使用System.Web.Services;
#端区
///
///小艇小结
///
[WebService(命名空间=”http://quality/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
公共级船只:WebService
{
#区域公共方法和运算符
///
///获取模型方法。
///
[网络方法]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共模型()
{
var models=Com.somedll.GetModels();
var serializer=新的JavaScriptSerializer();
var响应=模型。计数!=0
?序列化程序。序列化(模型)
:serializer.Serialize(新错误{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);
}
#端区
}
}
我收到以下错误发送HTTP标头后,服务器无法追加标头。
看看Firebug,对于每个请求,请求似乎都被发送了多次,至少4,有人能帮忙吗。如果您需要更多信息,请告诉我。移动您的
响应。请在呼叫刷新之前写下呼叫
this.Context.Response.Write(response);
this.Context.Response.Flush();
UPDATE:您还可以尝试删除ContentType
setter,因为您已经声明您的响应类型是JSON。您可以尝试将其从以下内容中剥离出来:
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();
编辑
听起来你可能正在经历这种行为:
你给repsonse写点什么
你脸红了吗
您的代码会触发一个错误,asp.net尝试重写标头,这是不允许的,因为对Flush()的调用已经写入了标头
在那里放一个试扣,看看是否有东西抛出。冲水应该是最后一件事吗?尝试交换顺序:this.Context.Response.Flush();this.Context.Response.Write(Response)@BobTodd仍然收到相同的错误。发送HTTP标头后,服务器无法追加标头。非常奇怪,谢谢你的更正,guysI已经删除了内容类型声明,这没有任何效果,我正在努力找出问题所在,页面看起来很好,响应(在你的帮助下)现在看起来很好。但是我仍然得到错误?我已经尝试过了,这已经确认我的web服务中没有错误(在框中打勾:)。这让我再次检查javascript,当我从中删除contentType时,我得到的数据被识别为JSON,但我仍然得到FireBug中显示的递归。我不知道为什么,但现在响应已更改为401,对所有6个调用都未经授权。只是重申一下,我只给服务打了一次电话