简体   繁体   English

如何在客户端接收响应?

[英]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.

相关问题 如何在客户端获取body response? - How to obtain the body response in the client-side? 如何在服务器端调用 gRPC stream 之前在客户端接收数据 - How can I receive data on client side before calling .end() on the server side for a gRPC stream 如何在客户端 javascript 中查找服务器响应时间 - How to find server response time in client side javascript 如何通过Ajax将加密的响应从nodejs传递到客户端? - How to Pass the encrypted response from nodejs to client side through ajax? 如何在客户端的xml响应中替换&lt;和&gt; - How to replace &lt; and &gt; from the xml response in client side 泽西岛客户端接收pojo我们的字节 - Jersey client side receive pojo us bytes 在客户端接收重定向的 POST 请求 - POST request to receive redirect on client side Firebase,无法接收通知(客户端) - firebase,can not receive notifications(client side) 如何从服务器(Node JS和Express)的客户端上接收和使用JSON对象? - How do I receive and use a JSON object on the client-side from the server (Node JS and Express)? 如何使用Python(带有Websocket的服务器)和JavaScript(客户端)接收JSON数据 - How to receive JSON data with Python (server with websockets) and JavaScript (client-side)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM