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.

4
  • Are you running it directly by double clicking on index.html or main html file ? Commented Feb 14, 2017 at 11:18
  • yes, I am. Does that matter? Commented Feb 14, 2017 at 11:46
  • start the app on a server Commented Feb 14, 2017 at 12:06
  • i have just tried it and still doesn't work. Commented Feb 14, 2017 at 12:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.