簡體   English   中英

$ http.post()方法不適用於Angular JS

[英]$http.post() method not working in angular js

我正在嘗試使用spring,angularjs和hibernet將數據插入db。 在app.js文件中的控制器方法$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City)無法正常工作,我不知道我做錯了什么。

AngularDate.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html ng-app="taskManagerApp">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>AngularJS Task Manager</title>
        <script data-require="angular.js@*" data-semver="1.2.13" src="http://code.angularjs.org/1.2.13/angular.js"></script>
        <script src="<c:url value=" /resources/js/app.js "/>"></script>
    </head>

    <body>
        <div ng-controller="taskManagerController">
            <span>Add Task</span>
            <div>
                <div>
                    <table>
                        <tr>
                            <td> Name:</td>
                            <td>
                                <input type="text" ng-model="Name" />
                            </td>
                        </tr>
                        <tr>
                            <td>City:</td>
                            <td>
                                <input type="text" ng-model="City" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <button ng-click="addTask()" class="btn-panel-big">Add New Task</button>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </body>

</html>

app.js

var taskManagerModule = angular.module('taskManagerApp', []);
var t = angular.module('taskManagerController', []);

taskManagerModule.controller('taskManagerController', function ($scope, $http) {
    var urlBase = "http://localhost:8081/logistics";
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

    //get all tasks and display initially
    //add a new task
    $scope.addTask = function addTask() {
        if ($scope.Name == "" || $scope.City == "") {
            alert("Insufficient Data! Please provide values for  name and city");
        }
        else {
            alert("else");
            $http.post(urlBase + '/angular/insert/' + $scope.Name + '/' + $scope.City).success(function (data) {
                 alert("Task added");
                 // $scope.tasks = data;
                 $scope.Name = "";
                 $scope.City = "";
                 //$scope.toggle='!toggle';    
             });
        }
    };

    // Archive Completed Tasks
});

//Angularjs Directive for confirm dialog box

誰能幫我解決我的問題?

$http帖子需要POST正文,因此如果有,請替換它

$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City)

//JSON data to post
var postData = {
  ...
  ...
 }
$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City, postData).success(
function(data) {
    //success code
 })

或者如果您沒有任何發布數據,則只需發送一個空的JSON對象,例如

 $http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City,{}).success(
    function(data) {
        //success code
     })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM