2

I'm new with AngularJS and I'm trying to learn how to persist in my DB. I'm working without server, just using my local files. I have seen in several tutorials how to do my $http.post function but it doesn't work. This is my app.js:

var app = angular.module("TestIdoneidadApp", []);
            app.controller("TIController", ['$scope','$http', function($scope, $http) {
                $scope.gestor= '';
                $scope.entidad= ''; 
                $scope.save=function(){

                    $scope.xml_object = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><testIdoneidad>';
                    $scope.xml_object += '<gestor>' + $scope.ti.gestor + '</gestor>';
                    $scope.xml_object += '<entidad>' + $scope.ti.entidad + '</entidad>';
                    $scope.xml_object += '</testIdoneidad>';
                        console.log($scope.xml_object);
                        $http.post("insert.php", {'xml_object':$scope.xml_object});
                    }
            }]);

I'm using Chrome to run this and it tells me I can't use this kind of path, so I uploaded my php file to github, but it still doesn't work.

This is my php file:

<?php header("Access-Control-Allow-Origin: *"); 
header("Access-Control-Allow-Headers: Authorization, Content-Type");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE");
$data = json_decode(file_get_contents("php://input"));
$xml_object = mysql_real_escape_string($data->xml_object);
mysql_connect("localhost","msandbox","msandbox");
mysql_select_db("angular_db");
mysql_query("INSERT INTO test_idoneidad(xml_object) VALUES($xml_object)");
?>

I have seen a lot of errors with CORS, that's why I have used Access-Control-Allow-Origin.

When I use my github php file, i get this error:

XMLHttpRequest cannot load https://raw.githubusercontent.com/Asdf/insertPhp/master/insert.php. Response for preflight has invalid HTTP status code 400

I don't know how to fix this. May you help me?

Thanks in advance.

DMC19
  • 825
  • 2
  • 14
  • 33

0 Answers0