0

I'm using XHR 2 to upload/save files.

According to the response of the server I want to perform an action. For example if the responce is "Saved" I want to hide a div or if the response is "Not Saved" I want to show another div etc...

I implemented what appears to be a simple code that should be working , but is not

Here is the snippet of the XHR

    //initialize

     var xhr = new XMLHttpRequest();
          xhr.open('POST', 'upload.php');
          xhr.responseType="text";
          xhr.onload = function() {

                                  //if all ok.... 
                      if (xhr.status === 200)         
                      { 
                                       //update html5 progress bar                                   
                       progress.value = progress.innerHTML = 100;

                                       //get the respnse  
                      var data=xhr.response;

                                      //convert it to sting - kind of overkill, I know, but I'm stack
                           var data2=data.toString();

                                      //alert it -- works    
                      alert('data2     '+data2);



                                 //now, do something, according to the response -- NOT working, never alert anything
                               if (data2=="Not Saved"){alert('Ooops, not saved');}


                                if(data2=="Saved"){alert('It's all good');} 


                              if(data2=="File too big"){alert('hey, you are watching Jake and Amir');}  


                              document.getElementById('imagesaved').innerHTML=data; 


                     } 

//refers to if (xhr.status === 200)               
else {document.getElementById("imagesaved").innerHTML="Connect to server failed";}

What is wrong here? This should be working right? Any suggestions?

Thanks

EDIT

I put the alerts for testing. What I actually want to do is call some functions.

If I put

if (data2=="Not Saved"){functionOne();}


if(data2=="Saved"){functionTwo();}  


if(data2=="File too big"){functionThree();} 

the functions never get called

if I put

if (data2!="Not Saved"){functionOne();}


if(data2!="Saved"){functionTwo();}  


if(data2!="File too big"){functionThree();}

ALL the functions are called!!!

I still dont get it...Maybe its something with the response? Or the onload function?

Thanks again

4

2 回答 2

1

我最后做的是用数字而不是文本让服务器响应。所以编码不再重要......

这是代码

    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
                      if (xhr.status == 200)          
                      {                                      
                      var data=xhr.response;



                              if(data==1) 

         //say to  the user is saved
{document.getElementById('imagesaved').innerHTML="Saved";}  

    //say to the user, there was an error

    else{document.getElementById('imagesaved').innerHTML="Error";}


                                    }         
                        //say to the user that connection to the server failed
                     else {document.getElementById("imagesaved").innerHTML="Cannot connect";}
          };


          xhr.open('POST', 'upload.php');
     xhr.send(formData);

这是一种解决方法。从技术上讲,我不知道它是否是解决此问题的正确方法。无论如何,我决定发布它,以帮助其他人快速解决类似问题。如果其他男孩有更好的建议方式,请做。

于 2013-08-26T13:51:47.220 回答
0

在这一行 :if(data2=="Saved"){alert('It's all good');}中,您必须转义“'”。所以将其转换为:if(data2=="Saved"){alert('It\'s all good');}

你确定你的 ajax 的响应是 text/plain 吗?查看控制台(chrome 上的 ctrl+shift+i,firefox 上的 F12)、网络或网络选项卡。

如果您也遇到一些 javascript 错误,请查看控制台选项卡。

于 2013-08-25T10:12:31.723 回答