AJAX - 使用GET的POST而不是 [英] AJAX - Using POST instead of GET

查看:124
本文介绍了AJAX - 使用GET的POST而不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到现在为止,我一直在使用:

Up until now I have been using:

xmlhttp.open("GET","server_script.php?q="+str,true);

感谢

编辑: 我提供的任何一个解决方案,可能会遇到这个页面来演示如何使用POST而非GET。如果你是新的AJAX我建议这个教程 http://www.w3schools.com/PHP/ php_ajax_php.asp 使用GET方法第一。

I am providing a solution for anyone that may come across this page to demonstrate how to use POST instead of GET. If you are new to AJAX I would recommend this tutorial http://www.w3schools.com/PHP/php_ajax_php.asp using the GET method first.

解决方案 -

JavaScript的:

javascript:

xmlhttp.open("POST","script.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send('var_name='+str);

PHP的:

php:

$var_name = GET['var_name'];

echo $var_name;

有关使用POST的原因和GET - 看评论的打击。

For reasons regarding the use of POST and GET - see comments blow.

推荐答案

您显示查询可能是完全正常的GET请求。没有必要去改变它。

The query you show is probably perfectly fine as a GET request. No need to change it.

有理由使用了另一种:要求更改服务器端(即变化数据)状态一般应使用POST; 读的要求应该是GET。

There are reasons to use one over the other: Requests that change state on server side (i.e. change data) should generally use POST; "read" requests should be GET.

本配备了一个隐含的安全优势,因为你无法通过走私的URL到用户的页面(如,显示的图像的URL指向名为 deleteall.php <一个管理页面做任何损害/ code>)。

This comes with an implicit security advantage because you can't do any damage by smuggling an URL into a user's page (like, showing an image whose URL points to an admin page named deleteall.php).

如果您的要求只是检索数据,你完全没有住在GET。

If your request just retrieves data, you're perfectly fine staying with GET.

有关何时使用哪种了广泛的讨论见这个问题。 获得与POST的AJAX?

See this question for an extensive discussion on when to use which. GET vs POST in AJAX?

这篇关于AJAX - 使用GET的POST而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆