[英]How to send and receive Form Data by Javascript
First consider this: StackoverFlow link . 首先考虑一下: StackoverFlow link 。
Here, Ajax is used to open an Xhttp channel to the server, and send some data to the php script file using post method. 在这里,Ajax用于打开到服务器的Xhttp通道,并使用post方法将一些数据发送到php脚本文件。 I have a perl script file inside CGI-bin, but that should work too.
我在CGI-bin中有一个perl脚本文件,但这也应该起作用。
I want to send the data via Javascript to the Perl script, and receive it back without the page refreshing, so I did this: 我想通过Javascript将数据发送到Perl脚本,并在不刷新页面的情况下将其接收回来,所以我这样做了:
Javascript: 使用Javascript:
var basepath = "localhost";
var req = new XMLHttpRequest();
req.open("POST", basepath+"/perlweb/lox.pl", false);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send("script="+this.script);
req.onreadystatechange = function(){
// execute this when ready state changes, i.e. server responds
if (req.readyState == 4 && req.Status == 200) {
// we got what we wanted
console.log(req.responseText);
}
}
lox.pl
is my script name. lox.pl
是我的脚本名称。 It is in /perlweb under localhost. 它位于localhost下的/ perlweb中。
I have created this file in the same place under the name test.pl: 我已经在同一位置以test.pl名称创建了该文件:
#! /path/to/perl
print "Content-type: text/plain\n\n";
print "it now works\n";
Calling this with /localhost/perlweb/test.pl
produces the expected output. 使用
/localhost/perlweb/test.pl
调用它会产生预期的输出。 So I am thinking perl is ready as well. 所以我认为perl也已经准备好了。
Now, back to the javascript I have two things: 现在,回到javascript我有两件事:
I have this warning : 我有这个警告:
Synchrone XMLHttpRequests am Haupt-Thread sollte nicht mehr verwendet werden, weil es nachteilige Effekte für das Erlebnis der Endbenutzer hat. Für weitere Hilfe siehe http://xhr.spec.whatwg.org/
Translates to : Synchronous XMLHttpRequest in main thread should not be used any more, because it can have sustainable effects (?? sic) for the final result of the end users. 转换为:不应再使用主线程中的同步XMLHttpRequest,因为它可能对最终用户的最终结果产生可持续的影响。
I want to get rid of this, but I am not aware, where to start. 我想摆脱这个问题,但是我不知道从哪里开始。 Looking up the link xhr.spec.whatwg.org is confusing me.
查找链接xhr.spec.whatwg.org使我感到困惑。 That seems like a full specification document.
这似乎是完整的规范文档。
Could anyone please simply point me to what I am supposed to do? 有人可以简单地指出我应该做的事情吗?
And I have this error: 我有这个错误:
NS_ERROR_DOM_BAD_URI: Access to restricted URI denied
Although the file exists in my own machine (because the file exists in my own machine, I am assuming that I am not going to hit the CORS issue). 尽管该文件存在于我自己的计算机中(因为该文件存在于我自己的计算机中,所以我假设我不会遇到CORS问题)。
What is then causing the problem? 那是什么引起了问题呢?
To send asynchronously, change 要异步发送,请更改
req.open("POST", basepath+"/perlweb/lox.pl", false); // false - sync
to 至
req.open("POST", basepath+"/perlweb/lox.pl", true); // true - async
async - An optional boolean parameter, defaulting to true, indicating whether or not to perform the operation asynchronously.
async-可选的布尔参数,默认为true,指示是否异步执行操作。 If this value is false, the send() method does not return until the response is received.
如果该值为false,则send()方法在接收到响应之前不会返回。 ( Ref )
( 参考 )
Note: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), synchronous requests on the main thread have been deprecated due to the negative effects to the user experience.
注意:从Gecko 30.0(Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27)开始,由于对用户体验的负面影响,不赞成在主线程上执行同步请求。
This is why you are getting the warning "Synchronous XMLHttpRequest in main thread should not be used any more". 这就是为什么您收到警告“不应再使用主线程中的同步XMLHttpRequest”的原因。
NS_ERROR_DOM_BAD_URI: Access to restricted URI denied
NS_ERROR_DOM_BAD_URI:拒绝访问受限制的URI
I suggest changing 我建议改变
var basepath = "localhost";
to either 到
var basepath = "http://localhost"; or
var basepath = "/localhost";
because, if I'm not mistaken, you want to open either a URL, or a local path on your machine, but "localhost" by itself is probably neither. 因为,如果我没记错的话,您想在计算机上打开URL或本地路径,但是“ localhost”本身可能都不是。
If you are still having this error, please see "NS_ERROR_DOM_BAD_URI: Access to restricted URI denied" 如果您仍然遇到此错误,请参阅“ NS_ERROR_DOM_BAD_URI:拒绝访问受限制的URI”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.