在DNN 4.9.2网站中,我试图使用jquery调用服务器端方法。 我在以下代码中设置了请求标头“X-OFFICIAL-REQUEST”。 JavaScript代码......
jQuery.ajax({
type: "POST",
async: "false",
url: location.href,
dataType: "json",
data: ({'FUNCTION': 'FunctionName', 'param0': '1' }),
success: function(data) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
},
beforeSend: function(xhr) {
xhr.setRequestHeader("X-OFFICIAL-REQUEST", "TRUE");//Used to ID as a AJAX Request
},
complete: function(XMLHttpRequest, textStatus) {
}
});
在我正在处理请求的代码中
Response.Write(strData)
Response.Flush()
开启Response.Flush
我收到错误“在发送HTTP标头后,服务器无法附加标头。”
背后的代码......
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Request.Headers("X-OFFICIAL-REQUEST") Is Nothing Then
If Request.Headers("X-OFFICIAL-REQUEST").ToString().ToLower() = "true" Then
AjaxWrapper()
End If
End If
End Sub
Protected Sub AjaxWrapper()
Response.Clear()
Dim strData As String = String.Empty
Dim type As Type = [GetType]()
Dim method As MethodInfo = type.GetMethod(Request.Params("FUNCTION"))
Dim objs As Object() = New Object(method.GetParameters().Length - 1) {}
For i As Integer = 0 To objs.Length - 1
objs(i) = (New PortalSecurity()).InputFilter(Request.Params("param" & i), PortalSecurity.FilterFlag.NoMarkup)
Next
If method IsNot Nothing Then
strData = method.Invoke(Me, objs) 'This method is returning a test Message fr eg. Test Data
End If
Response.Write(strData)
Response.Flush()
Try
Response.Close()
Catch
End Try
Response.End()
Return
End Sub
答案 0 :(得分:1)
在服务器端,您必须输出一些文本,然后尝试发送标头。文本可能是换行符,空格或任何文本。您需要确保在发送标头后发生所有文本输出。您可以通过缓冲服务器上的输出来执行此操作,并且只有在您知道不需要发送任何标头后才能发送。