[英]Codeigniter + Angular issue with $http.post(). Codeigniter not receive data
我有一个奇怪的Codeigniter-Angular问题。 我的想法是设置控制器使其工作如下:
问题是$http.post()
发送正确的数据,我在Firebug中看到了它,但是CI没有捕获到它。
我更改标头参数,试图抓住file_get_contents("php://input")
和$this->input->post
,但始终为Null
。
任何想法?
我尝试了这个:
$http(
{
method: 'POST',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: data
});
有和没有头,transformData都发生了变化……什么也没有。
我试图将相同的数据发送到原始php脚本,并且它起作用了。 所以,以某种方式-Codeigniter阻止了这一点。
任何想法 ?
这是一个常见问题,让我们开始:将ngRoute脚本添加到您的文档中,并在您的应用模块中,添加以下代码
var myApp = angular.module('myApp', ["ngRoute"]).
config(["$httpProvider",function($httpProvider)
{
$httpProvider.interceptors.push(['$q', function($q) {
return {
request: function(config) {
if (config.data && typeof config.data === 'object') {
// Check https://gist.github.com/brunoscopelliti/7492579
// for a possible way to implement the serialize function.
config.data = serialize(config.data);
}
return config || $q.when(config);
}
};
}]);
var serialize = function(obj, prefix) {
// http://stackoverflow.com/questions/1714786/querystring-encoding-of-a-javascript-object
var str = [];
for(var p in obj) {
var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
str.push(typeof v == "object" ? serialize(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
return str.join("&");
}
}]).
run(function($http) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8;";
});
现在在您的postService上(或使用$ http的任何地方)
var postData = function(method,data) {
return $http.post("controller.php?/"+method,{data:data})
.then(function(response) {
return response.data;
},
function(response) {
//handle error
});
}
//use
postData("userLogin",{username:"username",password:"password"}).then(function(response){
});
现在,codeIgniter将获得input-> post(“ data”)(始终为数据);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.