1

在 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
4

1 回答 1

1

在服务器端,您必须输出一些文本,然后尝试发送标头。文本可能是换行符、空格或任何文本。您需要确保在发送标头后发生所有文本输出。您可以通过在服务器上缓冲输出来做到这一点,并且仅在您知道不需要发送任何标头后才发送。

于 2012-10-09T06:44:25.240 回答