0

I am posting to a php script via jQuery:

$.post('http://*****.php', {'clicked':'true'}, 'json');

Looking at the post data, it responds with:

Request URL:http://*****.php
Request Method:POST
Status Code:200 OK

The Form Data in the headers looks like:

clicked:true

When I attempt to retrieve the POST data on my PHP script via:

var_dump($_POST);

I get an empty array:

array(0) { }

However, when playing around with the command line, I can successfully view the post data from the response when I do:

wget --post-data 'clicked=true' http://****.php

The successful response with wget is:

array(1) {
  ["name"]=>
  string(4) "test"
}

I have been unable to figure it out... and it's not my HTACCESS file. Also, GET requests work just fine. I'm assuming my jQuery POST is wrong, but don't know what it could be - it comes directly from the jQuery docs.

4

2 回答 2

3

当 POST 数据作为 JSON 字符串发送时,$_POST不会显示数据。要访问它,您必须使用php://input.

$phpinput = file_get_contents("php://input");
if(!$phpinput) {
  //No data was sent
}
else {
  //Do something with yo data
}

有关此技术的详细信息,请查看以下评论​​之一中提供的出色答案。

于 2013-03-19T15:57:56.123 回答
0

改用原始 $.ajax

$.ajax({

url : 'http://*.php', data : {'clicked':'true'}, dataType : 'json', method : 'post'

});

于 2013-03-19T16:16:26.097 回答