I'm fairly new to AngularJS and I am facing an issue in my Ionic app where I am trying to send a JSON to my server. Here is the code snippet from the controller:
`enter code here`.controller('DashCtrl', ['$scope','$http',function($scope, $http) {
$scope.formData = {};
$scope.sendForm = function(){
$http({
method : 'POST',
url : 'http://www.---.org/appForm.php',
data : JSON.stringify($scope.formData),
headers : {'Access-Control-Allow-Origin':'*'}
})
.success(function(data) {
console.log(data);
if (!data.success) {
$scope.errorName = data.errors.name;
$scope.errorSuperhero = data.errors.superheroAlias;
} else {
$scope.message = data.message;
}
});
};
However, when I try to send the POST request, the console displays the following error messages:
-[Error] Failed to load resource: Origin htt.. is not allowed by Access-Control-Allow-Origin. (appForm.php, line 0)
-[Error] XMLHttpRequest cannot load htt.. Origin http://localhost:8100
is not allowed by Access-Control-Allow-Origin. (localhost, line 0)
I attempted to add this .htaccess file in my home directory:
Header add Access-Control-Allow-Origin "*" Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type" Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
but it resulted in another error:
-Failed to load resource: the server responded with a status of 500 (Internal Server Error).
I'm feeling quite frustrated! Does anyone have any suggestions or ideas? Thank you for your attention.