$ _ POST不会$ HTTP POST很好地工作 [英] $_POST doesn't work well with $http POST
问题描述
在我controller.js我喜欢这样
in my controller.js I do like this
$http({
url: 'http://example.com/api.php',
method: "POST",
data: { 'YTParam' : 'test' }
})
.then(function(response) {
console.log(response.data);
},
function(response) { // optional
// failed
}
);
我甚至检查网络工具,帕拉姆也过去了。
I even checked the network tool, the param did passed.
在我api.php我尝试回声$ _ POST [YTParam]
但在我的控制台我什么也看不见?什么是错在这里?我试着回声串,它没有出现..
and in my api.php I try echo $_POST["YTParam"]
but in my console I see nothing? What's wrong here? I tried to echo "string", it did appeared..
推荐答案
您的数据不是 $ _ POST
数组中,因为你的角度JS的contentType设置为应用程序/ JSON
,和PHP预计应用程序/ x-WWW的形式urlen codeD
为POST请求。 从文档报价的:
Your data is not in the $_POST
array, because your angular JS contentType is set to application/json
by default, and PHP expects application/x-www-form-urlencoded
for a POST request. Quoting from docs :
$ httpProvider.defaults.headers.post:(头默认POST请求)
$httpProvider.defaults.headers.post: (header defaults for POST requests)
内容类型:应用程序/ JSON
Content-Type: application/json
要改变这一点,无论是在你的配置修改这个值:
To change this, either change this value in your config :
yourApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
}]);
在当时还是你把你的要求:
Or at the time you send your request :
$http({
url: 'http://myURL.com/api/',
method: "POST",
headers: {
'Content-Type': "application/x-www-form-urlencoded"
},
data: { 'YTParam' : 'test' }
})
您可以设置的contentType
直接作为属性,但我不是100%肯定这一点。
You can set contentType
directly as a property, but I'm not 100% sure of this.
这篇关于$ _ POST不会$ HTTP POST很好地工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!