javascript 将数据从浏览器发送到服务器并返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19014257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending data from a browser to a server and back
提问by user18490
I am C++ developer and I haven't really followed up on any development related to the web for a very long time. I have this project I would like to implement, really as a goal to sort of catch up on these technologies. My project is this:
我是 C++ 开发人员,很长时间没有真正跟进任何与 Web 相关的开发。我有一个我想实施的项目,实际上是为了赶上这些技术。我的项目是这样的:
- display some content in a browser (for example the content of a 3D scene using a canvas and WebGL), have a button on the page. When the button is clicked, send the data to the server (the camera position for instance), render the image on the server (using some offline high-quality rendering solution), return the image back to the browswer, and finally, display it in the canvas.
- 在浏览器中显示一些内容(例如使用画布和 WebGL 的 3D 场景的内容),在页面上有一个按钮。当按钮被点击时,将数据发送到服务器(例如相机位置),在服务器上渲染图像(使用一些离线高质量渲染解决方案),将图像返回到浏览器,最后显示它在画布中。
I believe I can fill up the gaps with simple things such as WebGl, canvas and HTML 5. I am familiar with some of these techniques and I can learn. Where I am completely lost is the technology that is used or needed to do things such as sending the data to a server, having them processed there, and sending some result back to the client. Now I have done some research on the Web of course, but the is SO MUCH STUFF out there, that it's just REALLY hard to know in which direction going. They are tons of libraries, APIs, bits of technologies, etc.
我相信我可以用 WebGl、canvas 和 HTML 5 等简单的东西来填补空白。我熟悉其中的一些技术,我可以学习。我完全迷失的是用于或需要做一些事情的技术,例如将数据发送到服务器,在那里处理它们,并将一些结果发送回客户端。现在我当然在网络上做了一些研究,但是那里的东西太多了,很难知道朝哪个方向发展。它们是大量的库、API、一些技术等。
I am suspecting I need to use some combination of JavaScript, DOM, HTML5 ... but would appreciate if people having done that before or knowing how this should work, could point me to the right direction. I am really looking for something basic, and IF possible not using some sort of 3rd party APIs. I don't want to do something complicated just simple, send data, process, send back for display. My goal is to understand the principles, not to make something professional or super powerful. I am doing this with an educational goal in mind (to learn and understand).
我怀疑我需要使用 JavaScript、DOM、HTML5 的某种组合......但是如果人们之前做过或者知道这应该如何工作,可以为我指明正确的方向,我会很感激。我真的在寻找一些基本的东西,如果可能的话,不使用某种 3rd 方 API。我不想做一些简单的复杂的事情,发送数据,处理,发回显示。我的目标是理解原理,而不是制作专业或超级强大的东西。我这样做是为了一个教育目标(学习和理解)。
I read about RESTFul but I am still unsure if this is what I need. Really if someone can either simply describes me the basic technology components that I need for this project, point me to documents, tutorials, examples, give me the name for the bits and pieces of technologies I should read about, it would be greatly appreciated.
我阅读了 RESTFul,但我仍然不确定这是否是我需要的。真的,如果有人可以简单地向我描述这个项目所需的基本技术组件,给我指点文档、教程、示例,给我应该阅读的零碎技术的名称,我将不胜感激。
I realize the scope of this question is very large (and that I should have done my home work before instead of having years now of knowledge to catch up on). I believe though this question can be of great interest to many. And I also promise that I will post my findings and why not my working example when I have one figured out and working.
我意识到这个问题的范围非常大(而且我应该在之前完成我的家庭作业,而不是现在有多年的知识来追赶)。我相信尽管这个问题可能引起很多人的极大兴趣。而且我还保证,当我找到并工作时,我会发布我的发现以及为什么不发布我的工作示例。
Thank you.
谢谢你。
采纳答案by cssyphus
NOT AN ANSWER, just suggestions/ideas that include code. A structured/formatted comment.
不是答案,只是包含代码的建议/想法。结构化/格式化的评论。
Unsure how to use/code them in C++, but this is just an issue of rendering HTML and implementing javascript code.
不确定如何在 C++ 中使用/编码它们,但这只是呈现 HTML 和实现 javascript 代码的问题。
The essentials are:
要点是:
Have jQuery lib loaded.One way is:
加载 jQuery 库。一种方法是:
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
Use a javascript code block for your jQuery script:
为您的 jQuery 脚本使用 javascript 代码块:
<script type="text/javascript">
$(document).ready(function() {
$('#mybutton').click(function() {
var pic = $('image_selector').val();
$.ajax({
type: "POST",
url: "ind.php",
data: "img=" + pic
})
.done(function(recd) {
$('#txtHint').html(recd);
});
}); //END mybutton click
}); //END document.ready
</script>
I don't know how you would send a pic as a var, or how to structure that, but you get the basic gist...
我不知道你将如何将图片作为 var 发送,或者如何构建它,但你得到了基本的要点......
On the server side, it works like this (using PHP for eg):
在服务器端,它是这样工作的(例如使用 PHP):
<?php
$image = $_POST['img'];
//Do something with the received image
Actually, now that I'm thinking about it, you are sending an image (something I haven't done before), so I don't think you can just send it like text or a JSON object... You may need to post it with the enctype='multipart/form-data
attribute for file uploads, as you do when using a form for uploads? Just guessing.
实际上,现在我正在考虑它,您正在发送图像(我以前从未做过的事情),所以我认为您不能像文本或 JSON 对象一样发送它......您可能需要将它与enctype='multipart/form-data
文件上传的属性一起发布,就像您在使用表单上传时所做的那样?只是猜测。
Anyway, this is not intended to answer your question, just to give you some hints as to where to look further.
无论如何,这不是为了回答您的问题,只是为了给您一些关于在哪里进一步查看的提示。
See these simplistic examples for the basics of AJAX:
有关 AJAX 的基础知识,请参阅这些简单的示例: