我的应用程序中有一个网页,需要调用我设置的 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 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/