Topic
  • 4 replies
  • Latest Post - ‏2014-09-20T11:54:16Z by jitupkp
timwatson
timwatson
1 Post

Pinned topic XMLHttpRequest not receiving response

‏2011-09-26T15:04:24Z |
Hi there I am currently using a file uploader on my website to upload files to my database. Before the file is uploaded I need to carry out some validation that can only be done server side as it needs to access the database. The uploader is using XMLHttpRequest and my application uses ASP.Net web forms. The problem is that if the validation fails server side the returned response is not received client side. It works correctly if the file is uploaded or even if the validation is carried out after the file is uploaded. Anyway below is a simple example of what im doing.

Javascript

var xhr = this._xhrsid = new XMLHttpRequest();

xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
self._onComplete(id, xhr);
}
};

xhr.open("POST", queryString, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-File-Name", encodeURIComponent(name));
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.send(file);

VB

Sub ProcessRequest(ByVal context As HttpContext) Implements HttpHandler.ProcessRequest
Dim fileName As String = context.Request.QueryString("qqfile")

if Not Database.IsValid(fileName) then
begin
context.Response.Write("{error:""Invalid file""}")
Return
end If

Dim newFile As New FileStream(strPath + "\" + fileName, FileMode.Create)
Dim body As Byte() = context.Request.BinaryRead(context.Request.TotalBytes)
newFile.Write(body, 0, body.Length)
newFile.Flush()
newFile.Close()
context.Response.Write("{success: true}")

End Sub

I found that I am only receiving readyState 1 and 4 when returning the error message. I read that its not possible to set the responseText when the readyState is equal to 1 or 2. Could I be trying to set the responseText when the readyState is not 3 or 4? If so is there a way of causing the readyState to change to 3 or 4 server side before setting the response text?
Updated on 2011-10-13T20:39:30Z at 2011-10-13T20:39:30Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    1140 Posts

    Re: XMLHttpRequest not receiving response

    ‏2011-10-08T12:51:09Z  
    [1] First make sure you don't have typo on the name of the interface the sub is implementing...
    [2] Add a condition wrapping the whole thing on the existing of the query "qqfile".
    
    If Not String.IsNullOrEmpty(context.Request.QueryString(
    "qqfile"))Then 
    'etc etc... End If
    

    [3] Provision a structure of Try... Catch... End Try to control the code if it can throw such as IOException etc...
  • SystemAdmin
    SystemAdmin
    1140 Posts

    Re: XMLHttpRequest not receiving response

    ‏2011-10-13T20:39:30Z  
    Is a callback method registered with the XHR object?
    Please refer
    http://www.devx.com/webdev/Article/33024/1954
  • jitupkp
    jitupkp
    104 Posts

    Re: XMLHttpRequest not receiving response

    ‏2014-09-20T11:54:02Z  

    see more

    http://techgurulab.com/course/ajax-quiz-online/

  • jitupkp
    jitupkp
    104 Posts

    Re: XMLHttpRequest not receiving response

    ‏2014-09-20T11:54:16Z  
    [1] First make sure you don't have typo on the name of the interface the sub is implementing...
    [2] Add a condition wrapping the whole thing on the existing of the query "qqfile".
    <pre class="jive-pre"> If Not String.IsNullOrEmpty(context.Request.QueryString( "qqfile"))Then 'etc etc... End If </pre>
    [3] Provision a structure of Try... Catch... End Try to control the code if it can throw such as IOException etc...

    see more

    http://techgurulab.com/course/ajax-quiz-online/