function obtenerSesion() {
return $http.post(loginUrl,
{params: {
username: 'prueba123',
password: 'prueba123'}
})
.then(function(response) {
debugger;
canchas = response.data;
return canchas;
})
.catch(generarError);
}
在我的PHP中我有这个:
.
.
22. $nombreUsuario= $_POST["username"];
23. $contrasenia= $_POST["password"];
.
.
我收到回复状态" 200"但是当php执行时,error.txt会捕获这个:
第22行未定义的索引
你能告诉我,为什么我的php没有收到数据? 谢谢!
// // EDIT
的login.php
<?php
$response = array();
require("Db.class.php");
require("password.php");
require("jwt.php");
$settings = parse_ini_file("settings.ini.php");
$securityToken= '' . $settings["token"]. '';
$db = new Db();
$nombreUsuario= $_POST["username"]; //email o user
$contrasenia= $_POST["password"];
// $hash = password_hash($contrasenia, PASSWORD_BCRYPT);
$result = $db->query("SELECT ....);
if(count($result) == 0){
...
...
如果我硬编码$ nombreUsuario =&#39; prueba123&#39;和$ contrasenia =&#39; prueba123&#39; php完美无缺!
答案 0 :(得分:3)
您正在发送此对象:
{"params":{"username":"prueba123","password":"prueba123"}}
而不是:
{"username":"prueba123","password":"prueba123"}
正确的代码:
function obtenerSesion() {
return $http.post(loginUrl,
{
username: 'prueba123',
password: 'prueba123'
})
.then(function(response) {
debugger;
canchas = response.data;
return canchas;
})
.catch(generarError);
}
然后在PHP中读取原始输入并反序列化JSON,如下所示:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$nombreUsuario = $request->username;
$contrasenia = $request->password;
编辑://
聊天对话之后这是我提供给他的代码:
function obtenerSesion() {
return $http({
method: 'POST',
url: loginUrl,
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: {username: 'prueba123', password: 'prueba123'}
}).then(function successCallback(response{
debugger;
canchas = response.data;
return canchas;
}, function errorCallback(response) {
//do something
console.log(response);
});
我们更改了编码请求应用程序/ x-www-form-urlencoded以满足其特定需求。