我的應用程序中有一個Web頁面,需要調用我設置爲返回對象列表的Web服務。該呼叫建立像這樣ASP.NET JQuery AJAX POST返回數據但在401響應中
$(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標頭已經發送服務器不能追加頭。 看着螢火蟲的請求似乎被髮送多次至少爲每個請求,任何人都可以幫助。請讓我知道您是否需要更多信息
Flush應該是最後一件事情?嘗試交換順序:this.Context.Response.Flush(); this.Context.Response.Write(response); – 2012-03-28 13:40:02
@BobTodd仍然收到相同的錯誤**服務器在發送HTTP頭之後無法附加頭信息**真的很奇怪,謝謝你們的糾正 – Deviland 2012-03-28 13:50:03