She*_*don 1 html javascript php ajax
到目前为止,我一直在使用:
xmlhttp.open("GET","server_script.php?q="+str,true);
Run Code Online (Sandbox Code Playgroud)
谢谢
编辑:我正在为可能遇到此页面的任何人提供解决方案,以演示如何使用POST而不是GET.如果您不熟悉AJAX,我会首先使用GET方法推荐本教程http://www.w3schools.com/PHP/php_ajax_php.asp.
解-
JavaScript的:
xmlhttp.open("POST","script.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send('var_name='+str);
Run Code Online (Sandbox Code Playgroud)
PHP:
$var_name = GET['var_name'];
echo $var_name;
Run Code Online (Sandbox Code Playgroud)
有关使用POST和GET的原因 - 请参阅评论.
这是你如何使用帖子:
var url = "server_script.php";
var params = "q="+str;
xmlhttp.open("POST", url, true);
//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.send(params);
Run Code Online (Sandbox Code Playgroud)
您显示的查询作为 GET 请求可能非常好。没有必要改变它。
使用一个而不是另一个是有原因的:在服务器端更改状态(即更改数据)的请求通常应该使用 POST;“读取”请求应该是 GET。
这带有隐含的安全优势,因为您不能通过将 URL 走私到用户页面(例如,显示其 URL 指向名为 的管理页面的图像)来造成任何损害deleteall.php。
如果您的请求只是检索数据,那么您完全可以使用 GET。
有关何时使用 which 的广泛讨论,请参阅此问题。AJAX 中的 GET 还是 POST?
| 归档时间: |
|
| 查看次数: |
5865 次 |
| 最近记录: |