【问题标题】:AngularJS Form Mail - SyntaxError: Unexpected token '<'AngularJS 表单邮件 - SyntaxError: Unexpected token '<'
【发布时间】:2016-12-02 14:25:46
【问题描述】:

我正在尝试使用以下配置从我的 AngularJS 网站发送电子邮件:

联系方式.index.html:

<form  name="userForm"  class="well form-search">
    <input type="text" ng-model="name" class="input-medium search-query" placeholder="Name" required >
    <input type="email" ng-model="email" class="input-medium search-query" placeholder="Email" required >
    <input type="text" ng-model="message" class="input-medium search-query" placeholder="Message" required >
    <button type="submit" class="btn" ng-click="formsubmit(userForm.$valid)"  ng-disabled="userForm.$invalid">Submit </button>
</form>
<pre ng-model="result">
    {{result}}
</pre>

Contact.controller.js:

'use strict';

angular.module('core').controller('ContactController', ['$scope', '$rootScope', '$stateParams', '$http', '$window',
    function($scope, $rootScope, $stateParams, $http, $window) {
        $scope.url = '../../resources/submit.php';
        $scope.formsubmit = function(isValid) {
            if (isValid) {
                $http.post($scope.url, {"name": $scope.name, "email": 'admin@website.com', "message": $scope.message}).
                        success(function(data, status) {
                            console.log(data);
                            $scope.status = status;
                            $scope.data = data;
                            $scope.result = data;
                        })
            }else{
                  alert('Form is not valid');
            }
        }
    }
]);

提交.php:

<?php
    $post_date = file_get_contents("php://input");
    $data = json_decode($post_date);

    //print the values
    echo "Name : ".$data->name."\n";
    echo "Email : ".$data->email."\n";
    echo "Message : ".$data->message."\n";
?>

查看我的站点控制台时收到以下错误:

[Error] SyntaxError: Unexpected token '

我尝试将我的 php 文件包装在以下内容中(没有帮助)

angular.callbacks._0 ( { })

我的文件结构如下:

dist/
├── bower_components
│   ├── ...
├── css
│   └── app.css
├── index.html
├── js
│   └── app.js
└── resources
    ├── ...
    └── submit.php

【问题讨论】:

  • PHP 代码是在服务器上实际执行的,还是只是作为纯文本返回?你如何提供你的 PHP 文件?
  • 这一切都在本地主机网络服务器上运行。
  • 如果您在浏览器中直接导航到 PHP 文件,您会看到什么?
  • 如果我访问localhost:8000/resources/action.php文件下载
  • 好吧,那么 PHP 并没有被执行,而是服务器直接提供文件内容。查看具有 PHP 运行时的 Web 服务器。 XAMPP 可能是一个很好的起点。

标签: javascript php angularjs


【解决方案1】:

经过进一步检查,我收到的错误:

[Error] SyntaxError: Unexpected token '<' (anonymous function) (submit.php:1)

是因为试图在不支持 PHP 的本地服务器上测试我的 PHP 脚本。

对于遇到同样问题的任何人,请尝试将您的网站文件上传到支持 PHP 的服务器进行测试。

【讨论】:

    猜你喜欢
    • 2016-01-31
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2017-06-22
    • 2014-04-23
    相关资源
    最近更新 更多