[英]cannot manipulate response from xmlHttpRequest 2
I'm using XHR 2 to upload/save files. 我正在使用XHR 2上载/保存文件。
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...
例如,如果响应为“已保存”,我想隐藏一个div,或者如果响应为“未保存”,我想显示另一个div,等等。
I implemented what appears to be a simple code that should be working , but is not 我实现了似乎应该可以工作的简单代码,但是没有实现
Here is the snippet of the XHR 这是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?
还是onload函数?
Thanks again 再次感谢
What I finally did is make the server response with numbers, not text. 我最后要做的是使服务器使用数字而不是文本进行响应。 So encoding does not matter any more...
因此编码不再重要...
This is the code 这是代码
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);
This is a workaround. 这是一种解决方法。 I dont know if its the right way to solve this problem , technically.
我不知道从技术上来说这是否是解决此问题的正确方法。 I decided to post it anyway, to help others to quickly solve similar problems.
我决定无论如何都要发布它,以帮助其他人快速解决类似的问题。 If anyboy else has a better way to suggest , please do.
如果还有其他人有更好的建议方法,请这样做。
In this line : if(data2=="Saved"){alert('It's all good');}
, you have to escape " ' ". 在这一行:
if(data2=="Saved"){alert('It's all good');}
,您必须转义“”。 So convert it to : if(data2=="Saved"){alert('It\\'s all good');}
因此将其转换为:
if(data2=="Saved"){alert('It\\'s all good');}
Are you sure that the response of your ajax is text/plain ? 您确定ajax的响应是text / plain吗? Look on the console (ctrl+shift+i on chrome, F12 on firefox), on net or network tab.
在[网路]或[网路]标签上,在控制台上(Chrome上为Ctrl + Shift + i,Firefox为F12)。
Look on console tab if you got some javascript errors too. 如果您也遇到一些JavaScript错误,请查看控制台标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.