2015-01-14 16 views
1

这是我的http获取代码,在这里console.log(数据)打印整个php代码而不是回显语句。请帮助我,我无法弄清楚发生了什么。我正在制作一个离子角应用程序。Ajax响应打印整个PHP代码写在PHP文件中,该怎么办?

角代码

// define angular module/app 
var formApp = angular.module('formApp', []) 
// create angular controller and pass in $scope and $http 
.controller('formController', function ($scope, $http) { 
    // create a blank object to hold our form information 
    // $scope will allow this to pass between controller and view 
    $scope.formData = {}; 
    // process the form 
    $scope.processForm = function() { 
     $http({ 
      method : 'GET', 
      url  : 'process.php', 
      data : $.param($scope.formData), // pass in data as strings 
      headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) 
     }) 
      .success(function(data) { 
       console.log(data); 
       if (!data.success) { 
        // if not successful, bind errors to error variables 
        $scope.errorName = data.errors.name; 
        $scope.errorSuperhero = data.errors.superheroAlias; 
       } else { 
        // if successful, bind success message to message 
        $scope.message = data.message; 
       } 
      }); 
    }; 
}); 

PHP代码

<?php 
$errors   = array();  // array to hold validation errors 
$data   = array();  // array to pass back data 
// validate the variables ====================================================== 
if (empty($_POST['name'])) 
    $errors['name'] = 'Name is required.'; 
if (empty($_POST['superheroAlias'])) 
    $errors['superheroAlias'] = 'Superhero alias is required.'; 
// return a response =========================================================== 
// response if there are errors 
if (! empty($errors)) { 
    // if there are items in our errors array, return those errors 
    $data['success'] = false; 
    $data['errors'] = $errors; 
} else { 
    // if there are no errors, return a message 
    $data['success'] = true; 
    $data['message'] = 'Success!'; 
} 
// return all our data to an AJAX call 
echo json_encode($data); 

控制台输出/ Ajax响应

<?php 
$errors   = array();  // array to hold validation errors 
$data   = array();  // array to pass back data 
// validate the variables ====================================================== 
if (empty($_POST['name'])) 
    $errors['name'] = 'Name is required.'; 
if (empty($_POST['superheroAlias'])) 
    $errors['superheroAlias'] = 'Superhero alias is required.'; 
// return a response =========================================================== 
// response if there are errors 
if (! empty($errors)) { 
    // if there are items in our errors array, return those errors 
    $data['success'] = false; 
    $data['errors'] = $errors; 
} else { 
    // if there are no errors, return a message 
    $data['success'] = true; 
    $data['message'] = 'Success!'; 
} 
// return all our data to an AJAX call 
echo json_encode($data); 

正如你可以看到它发送整个php代码而不是回显语句。

+1

如果php代码是按原样发送并且未执行/评估,那么这显然是服务器端的问题,而不是js,ajax或angularjs问题。 –

+0

离子和角是客户端和您的问题无关。此外,您的'php'文件的内容并不重要,因为php代码我根本没有评估过。正如我已经说过,这是你的服务器上的一个问题,或者没有安装php或者配置错误。 –

+0

soo如何配置?由于我对php很陌生,因此对其配置没有太多了解。所有 – Anky

回答

0

我知道这个线程是老了,但尝试检查.htaccess文件,如果你写下来的规则,如:

使用PHP5.4默认

AddHandler的应用程序/ x-httpd- php54 .php

摆脱它。我犯了这个错误。