在 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