[英]How to receive response on client side?
I have this form to upload an xml file to server, I am using fiddler to monitor each req and resp. 我有这种形式将xml文件上传到服务器,我正在使用提琴手来监视每个req和resp。 So the server sends me a small xml and i would like to receive it in my javascript as
XMLHttpRequest
makes it happen 因此,服务器向我发送了一个小的xml,而我想在我的JavaScript中接收它,因为
XMLHttpRequest
使其实现了
Note: I am uploading a file so enctype="multipart/form-data"
注意:我正在上传文件,所以
enctype="multipart/form-data"
var client;
var url_action = "/csm/create.action";
var dataString;
if (window.XMLHttpRequest) {
client = new XMLHttpRequest();
} else {
client = new ActiveXObject("Microsoft.XMLHTTP");
}
if (client.readyState == 4 && client.status == 200) {
alert(client.responseTest);
}
client.open("POST", url_action, true);
client.setRequestHeader("enctype", "multipart/form-data");
client.send();
My question is how can i receive the response from server side to JS variable. 我的问题是如何从服务器端收到对JS变量的响应。 In the above code
XMLHttpRequest
i don't think i can send a multipart request (file upload). 在上面的代码
XMLHttpRequest
我认为我无法发送多部分请求(文件上传)。 So any alternative is welcome. 因此,任何其他选择都值得欢迎。 Whichever solution provides me a response is good.
不管哪种解决方案都能给我一个好的答复。 Here is what i am doing, to submit the form.
这是我正在做的,要提交表格。 Thanks :)
谢谢 :)
var url_action="/csm/create.action";
$('#mainForm').attr('action', url_action);
$('#mainForm').submit();
Updated with solution 更新了解决方案
$(data).find('com\\.abc\\.db\\.ConfigInfo').each(function(){
cfgid=$(this).find('cfgId').text();
cfgname=$(this).find('cfgName').text();
filename=$(this).find('fileName').text();
timestamp=$(this).find('updateDate').text();
alert(cfgid+", "+cfgname+", "+filename+", "+timestamp);
});
You have jQuery available so don't ever create XHR objects manually. 您有可用的jQuery,所以永远不要手动创建XHR对象。 Besides that, you cannot use AJAX for file uploads unless you don't care about compatibility with certain browsers.
除此之外,除非您不关心与某些浏览器的兼容性,否则不能将AJAX用于文件上传。
Last but not least, you want to use the jQuery form plugin which will automatically fallback to a hidden iframe and a regular form if there is a file
input in the form. 最后但并非最不重要的一点是,您要使用jQuery表单插件 ,如果表单中有
file
输入,它将自动回退到隐藏的iframe和常规表单。 Note that you need to wrap your JSON response in <textarea></textarea>
for it to work properly though. 请注意,您需要将JSON响应包装在
<textarea></textarea>
中,以使其正常工作。 See http://jquery.malsup.com/form/#file-upload for details. 有关详细信息,请参见http://jquery.malsup.com/form/#file-upload 。 If you want to return XML you don't need to wrap it though - it should work fine without any server-side changes.
如果您想返回XML,则无需包装它-无需任何服务器端更改即可正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.