1
I have the following problem:
Xmlhttprequest cannot load
Whenever I send data from Angular
to the PHP
.
Man PHP
is like this:
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
$_POST = json_decode(file_get_contents('php://input'), true)
$name = $_POST['name'];
echo json_encode($name);
The angle is right..
var app = angular.module('myapp',[]);
app.controller('mycontroller', function($scope, $http){
$scope.sendPost = function (send) {
$http.post('http://localhost/meuSite/www/server/sendemail.php', send).then(function success(data){
console.log(data);
console.log(data.data);
}).then(function error(err){
if(err){
console.log(err);
}
});
}
});
And I put a file .htaccess
at the root with this content:
header set Access-Control-Allow-Origin "*"
For now, it’s all localhost. The front I’m running with http-server and back with apache do xampp
.
It’s hard to know where you failed, since you only have a few code snippets.
– Guilherme Nascimento
But the code is basically this. At first I just want to recover the data sent from the angular in PHP. I tested only the angle and ta receiving the data, so I did not put everything, and in php only has this snippet, the rest I removed to do this test and the code is + clean. I’ll set the whole angle.
– hisoka
@Guilhermenascimento may be from Chrome and localhost: http://stackoverflow.com/questions/10883211/deadly-cors-when-http-localhost-is-the-origin,https://www.thepolyglotdeveloper.com/2014/08/bypass-corserrors-testing-apis-locally/, https://github.com/deployd/deployd/issues/126 , https://www.thepolyglotdeveloper.com/2014/08/bypass-cors-errors-testing-apis-locally/
– Miguel
@Miguel can be, but for not being sure better wait for the AP edit and detail. Still the link of github is a great tip.
– Guilherme Nascimento
Friends, I already changed, I put the full angle and ad a photo with the error tbm.
– hisoka
Have you tried putting in the
.htaccess
the lineHeader set Access-Control-Allow-Headers "X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method"
? Error says request headerContent-Type
is not allowed.– Filipe Moraes
Felipe, bro, that was :D o. htaccess looks exactly like this: header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Headers "X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method" Mt obgd face, saved my day!
– hisoka