如何检查ajax调用方法是POST还是GET?

时间:2022-03-11 07:16:11

How do I check if the TYPE used in an AJAX call is POST or GET in my php page/server side?

如何检查AJAX调用中使用的类型是POST还是GET到php页面/服务器端?

5 个解决方案

#1


3  

Just try with:

试试:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  // POST
} else {
  // GET
}

#2


0  

in the php page echo the php variable like this echo 'post data'. $_POST['data']; echo 'get data'.$_GET['data']';

在php页面中,响应php变量,如“post data”。$ _POST['数据”);回波获取数据。$ _GET['数据']';

$.ajax({
      url: "test.html",
      context: document.body
    }).done(function(data) {
     //you can see it here whether its echoing(what) or not
alert(data);
    });

this way you will be able to know.

这样你就能知道了。

#3


0  

By the way, if you want your server side script to work with both GET and POST then you can use the $_REQUEST array instead of trying to find your variables in either $_GET or $_POST. $_REQUEST will always have the variables no matter type the call was.

顺便说一下,如果您希望服务器端脚本同时使用GET和POST,那么您可以使用$_REQUEST数组,而不是试图在$_GET或$_POST中查找变量。$_REQUEST将始终具有变量,无论调用是什么类型。

#4


0  

Use following

使用以下

if (isset{$_POST)) {
echo 'Post Values';
} else {
echo 'Get Values';
}

#5


0  

Better use $_SERVER['REQUEST_METHOD']:

更好的使用$ _SERVER(“REQUEST_METHOD”):

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // …
}

For more details please see the documentation for the $_SERVER variable.

有关更多细节,请参见$_SERVER变量的文档。

#1


3  

Just try with:

试试:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  // POST
} else {
  // GET
}

#2


0  

in the php page echo the php variable like this echo 'post data'. $_POST['data']; echo 'get data'.$_GET['data']';

在php页面中,响应php变量,如“post data”。$ _POST['数据”);回波获取数据。$ _GET['数据']';

$.ajax({
      url: "test.html",
      context: document.body
    }).done(function(data) {
     //you can see it here whether its echoing(what) or not
alert(data);
    });

this way you will be able to know.

这样你就能知道了。

#3


0  

By the way, if you want your server side script to work with both GET and POST then you can use the $_REQUEST array instead of trying to find your variables in either $_GET or $_POST. $_REQUEST will always have the variables no matter type the call was.

顺便说一下,如果您希望服务器端脚本同时使用GET和POST,那么您可以使用$_REQUEST数组,而不是试图在$_GET或$_POST中查找变量。$_REQUEST将始终具有变量,无论调用是什么类型。

#4


0  

Use following

使用以下

if (isset{$_POST)) {
echo 'Post Values';
} else {
echo 'Get Values';
}

#5


0  

Better use $_SERVER['REQUEST_METHOD']:

更好的使用$ _SERVER(“REQUEST_METHOD”):

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // …
}

For more details please see the documentation for the $_SERVER variable.

有关更多细节,请参见$_SERVER变量的文档。