1

In a DNN 4.9.2 web site ,I am trying to call a server side method using jquery. I am setting a Request Header "X-OFFICIAL-REQUEST" in following code. JavaScript Code ...

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) {
    }
});

While in the Code behind I am processing the request

Response.Write(strData)
Response.Flush()

On Response.Flush I am getting error "Server cannot append header after HTTP headers have been sent."

Code Behind...

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
sunshine
  • 125
  • 4
  • 15

1 Answers1

1

On the server side, you must be outputting some text, and then trying to send headers. The text might be a newline character, or a space, or any text at all. You need to make sure all text output happens after headers have been sent. You can do this by buffering the output on the server, and only sending after you know you don't need to send any headers.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • Thanks for your quick reply.Can you give me the code lines for this? – sunshine Oct 09 '12 at 06:47
  • you have posted client side code, the issue lies on the server side code. – Billy Moon Oct 09 '12 at 07:03
  • @Billiy..In server side code,If I am receiving the header value as true, then `code` Response.Clear() Response.Write("String Which I want to Sent Back") Response.Flush() this much I have done.and I am getting this error on Response.Flush(). Can you please tell me how to resolve this ? – sunshine Oct 09 '12 at 07:07
  • There is probably a newline or space leaking through. Maybe outside of the script tags, requiring no command to send them. It is impossible for me to tell you without you posting the server side code. – Billy Moon Oct 09 '12 at 07:21
  • `code` 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 – sunshine Oct 09 '12 at 07:30
  • 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 methos is returning a test Message 'Test Data' End If Response.Write(strData) Response.Flush() – sunshine Oct 09 '12 at 07:38
  • @Billy..I am so sorry fr the above scattered code..but I dnt knw how to indent the code..as this is my first post – sunshine Oct 09 '12 at 07:39
  • you should edit your post, and put the code in there. You should indent the code four spaces, so it gets formatted as code by stack overflow. – Billy Moon Oct 09 '12 at 08:03
  • @Billy... thanks... I indented the code..can you please look into it? – sunshine Oct 09 '12 at 08:52