简体   繁体   English

$http angularjs POST 不发送我的数据?

[英]$http angularjs POST don't send my data?

my angularjs $http我的 angularjs $http

userId = '1';

$http({
url: "php/loadTab.php",
method: "POST",
data: userId
}).success(function(data, status, headers, config) {
    console.log(data);
}).error(function(data, status, headers, config) {
});

heck, POST doesn't send the data to my php.哎呀,POST 不会将数据发送到我的 php。 I do echo $_POST['userId'] it returned undefined index.我确实 echo $_POST['userId'] 它返回了未定义的索引。 I also tried data:'userId':'1'我也试过data:'userId':'1'

Angular sends the data as JSON, not form encoded key/value pairs so you can set data to an object. Angular 将数据作为 JSON 发送,而不是形成编码的键/值对,因此您可以将data设置为对象。 You almost have it on your second try but you need to include the curly braces, which define an object:您几乎可以在第二次尝试时使用它,但您需要包含花括号,它定义了一个对象:

data: { 'userId' : userId }

Now, on the PHP side you need to access the raw POST data to decode the JSON like so:现在,在 PHP 端,您需要访问原始 POST 数据来解码 JSON,如下所示:

$data = json_decode(file_get_contents('php://input'));
echo $data->userId;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM