dousong2967 2015-02-08 10:57
浏览 104
已采纳

Angularjs和jquery不能以我的常规简单形式工作

I am learning Angularjs and I created simple form. Actually i am PHP developer so i preferred to use php as a server side scripting language. I am unable to submit the data to server, i tried so many methods but those are very complex if i am trying in standard method Angularjs is not working please check my code, and give me best method to work with angularjs, jquery and php. help me!

angular.module("mainModule", [])
  .controller("mainController", function($scope, $http) {
    $scope.person = {};
    $scope.serverResponse = '';

    $scope.submitData = function() {
      var config = {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded"
        }
      };

      $http.post("server.php", $scope.person, config)
        .success(function(data, status, headers, config) {
          $scope.serverResponse = data;
        })
        .error(function(data, status, headers, config) {
          $scope.serverResponse = "SUBMIT ERROR";
        });
    };
  });
<?php
 if (isset($_POST["person"]))
  {
    // AJAX form submission
    $person = json_decode($_GET["person"]);

    $result = json_encode(array(
      "receivedName" => $person->name,
      "receivedEmail" => $person->email));
  }  else
  {
    $result = "INVALID REQUEST DATA";
  }

  echo $result;

?>
<!DOCTYPE html>
<html>

<head>
</head>

<body ng-app="mainModule">
  <div ng-controller="mainController">
    <form name="personForm1" novalidate ng-submit="submitData()">
      <label for="name">First name:</label>
      <input id="name" type="text" name="name" ng-model="person.name" required />
      <br />
      {{person.name}}
      <br />
      <label for="email">email:</label>
      <input id="email" type="text" name="email" ng-model="person.email" data-parsley-type="email" required />
      <br />
      <br />
      <button type="submit">Submit</button>
    </form>
    <br />
    <div>
      {{$scope.serverResponse}}
    </div>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <!--<script type="text/javascript" src="script/parsley.js"></script>
  <script src="script.js"></script>-->
</body>

</html>

</div>
  • 写回答

3条回答 默认 最新

  • dp7311 2015-02-08 11:36
    关注

    Updated, this is code that was just tested with php and Apache - and it works. I also changed your server.php file like below. The file was created based on AngularJS Hub's Server Calls sample. The same source was used to create mainController.js' $http.post(...) method call so that it successfully posts data to the server.

    Screenshot (after submit)

    enter image description here

    server.php

    <?php
     if ($_SERVER["REQUEST_METHOD"] === "POST")
      {
    
       $result = "POST request received!";
    
      if (isset($_GET["name"]))
      {
        $result .= "
    name = " . $_GET["name"];
      }
    
      if (isset($_GET["email"]))
      {
        $result .= "
    email = " . $_GET["email"];
      }
    
      if (isset($HTTP_RAW_POST_DATA))
      {
        $result .= "
    POST DATA: " . $HTTP_RAW_POST_DATA;
      }
    
      echo $result;
      }
    
    ?>
    

    personForm.html

       <!DOCTYPE html>
        <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta charset="utf-8" />
            <title></title>
    
        </head>
            <body ng-app="mainModule">
                <div ng-controller="mainController">
                    <form name="personForm1" validate ng-submit="submit()">
                        <label for="name">First name:</label>
                        <input id="name" type="text" name="name" ng-model="person.name" required />
                        <br />
                        {{person.name}}
                        <br />
                        <label for="email">email:</label>
                        <input id="email" type="text" name="email" ng-model="person.email" required />
                        <br />
                        <br />
                        <button type="submit">Submit</button>
                    </form>
                    <br />
                    <div>
                        {{serverResponse}}
                    </div>
                </div>
    
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js"></script>
                <script src="mainController.js"></script>
                <!--<script type="text/javascript" src="script/parsley.js"></script>
                <script src="script.js"></script>-->
            </body>
    
    </html>
    

    mainController.js

    angular.module("mainModule", [])
      .controller("mainController", function ($scope, $http)
      {
      $scope.person = {};
    
      $scope.serverResponse = "";
    
      $scope.submit = function ()
      {
    
          console.log("form submit");
    
          var params = {
              name: $scope.person.name,
              email: $scope.person.email
          };
    
          var config = {
              params: params
          };
    
          $http.post("server.php", $scope.person, config)
          .success(function (data, status, headers, config)
          {
              console.log("data " + data + ", status "+ status + ", headers "+ headers + ", config " + config);
              $scope.serverResponse = data;
              console.log($scope.serverResponse);
          })
          .error(function (data, status, headers, config)
          { console.log("error");
              $scope.serverResponse = "SUBMIT ERROR";
    
    
           });
          };
      });// JavaScript source code
    

    Alternative way, with JSON handling:

    server_json.php

    <?php
      if ($_SERVER["REQUEST_METHOD"] === "POST")
      {
         /* code source: http://stackoverflow.com/a/22852178/2048391 */
         $data = array();
         $json = file_get_contents('php://input'); // read JSON from raw POST data
    
         if (!empty($json)) {
            $data = json_decode($json, true); // decode
         }
    
         print_r($data);
    
        }
    
      ?>
    

    Screenshot (after submit)

    Screenshot

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号