Ajax response printing whole php code written in php file, What to do?

942 Views Asked by At

This is my http get code, here console.log(data) is printing whole php code instead of echoed statement. Kindly help me guys i am not able to figure out whats happening. I am making an ionic angular app.

Angular Code

// 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 Code

<?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);

Console Output/Ajax Response

<?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);

As you can see it is sending the whole php code instead of echoed statement.

1

There are 1 best solutions below

0
On

I know this thread is old, but try to check your .htaccess file, if you have written down a rule like:

Use PHP5.4 as default

AddHandler application/x-httpd-php54 .php

Get rid of it. I made this mistake.