Ajax响应打印用php文件编写的整个php代码,该怎么办



这是我的http get代码,这里console.log(data)正在打印整个php代码而不是echoed语句。请帮助我,伙计们,我无法弄清楚发生了什么。我正在制作一个离子角应用程序。

角度代码

// 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代码而不是回显语句。

我知道

这个线程很旧,但如果您写下了这样的规则,请尝试检查您的 .htaccess 文件:

默认使用 PHP5.4

AddHandler application/x-httpd-php54 .php

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

最新更新