AngularJS $ http无响应

时间:2016-05-12 13:43:24

标签: php angularjs

我试图将newcontact保存到我的数据库中的一个表中,然后显示该表,但它不起作用,我找不到原因。当我点击保存按钮时,它甚至没有把任何东西放入数据库。 $ http.get也不起作用。

app.js

var app = angular.module("app", ["ngRoute"]);

app.controller("mainController", function($scope, $http, $location){

    $scope.newcontact = {};

    $scope.get = function(){
        $http.get("backend.php").success(function(response){
            $scope.list = response;
        });
    };

    $scope.save = function(){
        if($scope.newcontact.sauna){
            $scope.newcontact.sauna = "joo";
        }else{
            $scope.newcontact.sauna = "ei";
        }   
        $http({
            method: 'post',
            url: 'backend.php',
            data: $scope.newcontact,
            header: 'Access-Control-Allow-Origin: *'
        }).success(function(response){
            $scope.list = response;
            $scope.newcontact = {};
            $location.path("/list");
        });         
    };
});

backend.php

<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("localhost", "root", "", "harjoitus21");

if(file_get_contents("php://input")){

    $data = file_get_contents("php://input");
    $obj = json_decode($data);

    $nimi = strip_tags($obj->name);
    $email = strip_tags($obj->email);
    $ruoka = strip_tags($obj->ruoka);
    $sauna = strip_tags($obj->sauna);

    $conn->query("INSERT INTO ilmot (nimi, email, ruoka, sauna) VALUES ('$nimi', '$email', '$ruoka', '$sauna');");
}

$result = $conn->query("SELECT * FROM ilmot");

$resultArray = array();

if($result){
    while($row = $result->fetch_array(MYSQL_ASSOC)){
        $resultArray[] = $row;
    }
}

echo json_encode($resultArray);

form.html

<form role="form" ng-submit="save()">    
        <input ng-model="newcontact.name" type="text" class="form-control">

        <input ng-model="newcontact.email" type="text" class="form-control">

        <select ng-model="newcontact.ruoka" class="form-control">
            <option>Kala</option>
            <option>Liha</option>
            <option>Kasvis</option>
        </select>

        <input ng-model="newcontact.sauna" type="checkbox"> 

        <button type="submit" class="btn btn-default">Submit</button>
</form>

我已经检查过这些查询正在运行,并且backend.php位于与app.js相同的目录中,所以我认为它不能关于url。

0 个答案:

没有答案