weixin_33720956 2016-09-29 05:09 采纳率: 0%
浏览 39

jQuery,AJAX和AngularJS

I'm changing a Spring app from Thymeleaf to AngularJS (learning as I go) and this Ajax call that was previously working is now not even registering on in Chrome XHR. I don't have a lot of experience with these front-end frameworks so I'm not sure what changed.

index.html

            <!doctype html>
            <html ng-app="wishingWell">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport"
                      content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
                <meta http-equiv="X-UA-Compatible" content="ie=edge">
                <link rel="stylesheet" href="/css/normalize.css">


                <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
                <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-route.js"></script>
                <script   src="https://code.jquery.com/jquery-3.1.1.js"   integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="   crossorigin="anonymous"></script>
                <script src="/javascript/app.js"></script>

                <title>Wishing Well</title>
            </head>

            <body>
                    <nav class="nav">
                        <ul class="navback">
                            <li>
                                <a class="active" href="#/">The Well</a>
                            </li>
                            <li>
                                <a href="#/profile" >Profile</a>
                            </li>
                            <li>
                                <a href="#/sign-up">Sign Up</a>
                            </li>
                            <li>
                                <a href="#/login">Log In</a>
                            </li>
                            <li>
                                <a href="#/logout">Sign Out</a>
                            </li>
                        </ul>
                    </nav>

                    <div ng-view></div>

                    <script>
                        jQuery(document).ready(function($) {

                            $("#wish-form").submit(function(event) {

                                // Disble the search button
                                enableSearchButton(false);

                                // Prevent the form from submitting via the browser.
                                event.preventDefault();

                                $('#feedback').hide();
                                searchViaAjax();

                            });

                        });

                        function searchViaAjax() {

                            var search = {}
                            search["wish"] = $("#wish").val();

                            $.ajax({
                                type : "POST",
                                contentType : "application/json",
                                url : "/addwish",
                                data : JSON.stringify(search),
                                dataType : 'json',
                                timeout : 100000,
                                success : function(data) {
                                    console.log("SUCCESS: ", data);
                                    display(data);
                                },
                                error : function(e) {
                                    console.log("ERROR: ", e);
                                    display(e);
                                },
                                done : function(e) {
                                    console.log("DONE");
                                    enableSearchButton(true);
                                }
                            });

                        }

                        function enableSearchButton(flag) {
                            $("#btn-submit").prop("disabled", flag);
                        }

                        function display(data) {
                            var jsonString = JSON.stringify(data);
                            var json = JSON.parse(jsonString);
                            $('#feedback').html(json.msg);
                            $('#feedback').show();
                        }

                        $('#feedback').hover(function(){
                            $('#feedback').fadeOut(300, function(){
                                $(this).hide();
                            });
                        });

                    </script>
            </body>
            </html>

app.js

 var wishingWell = angular.module('wishingWell', [ 'ngRoute' ]);

 wishingWell.config(function($routeProvider, $httpProvider) {

$routeProvider.when('/', {
    templateUrl : 'home.html',
    controller : 'home',
    controllerAs: 'controller'
}).when('/login', {
    templateUrl : 'login.html',
    controller : 'navigation',
    controllerAs: 'controller'
}).otherwise('/');

$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';

 });



 wishingWell.controller('navigation',

 function($rootScope, $http, $location) {

    var self = this

    var authenticate = function(credentials, callback) {

        var headers = credentials ? {authorization : "Basic "
        + btoa(credentials.username + ":" + credentials.password)
        } : {};

        $http.get('user', {headers : headers}).then(function(response) {
            if (response.data.name) {
                $rootScope.authenticated = true;
            } else {
                $rootScope.authenticated = false;
            }
            callback && callback();
        }, function() {
            $rootScope.authenticated = false;
            callback && callback();
        });

    }

    authenticate();
    self.credentials = {};
    self.login = function() {
        authenticate(self.credentials, function() {
            if ($rootScope.authenticated) {
                $location.path("/");
                self.error = false;
            } else {
                $location.path("/login");
                self.error = true;
            }
        });
    };
});

My guess is that something within AngularJS is overriding jQuery, but I can't figure out what. Most of the questions about this I've seen relate to using jQuery Ajax within an AngularJS controller, but it seems like it should work outside. Also, the form data being submitted via Ajax gets put into the ng-view.

  • 写回答

1条回答 默认 最新

  • helloxielan 2016-09-29 05:59
    关注

    There a a few things amiss here:

    • The jQuery(document).ready(function($) is fired when the browsers emits the DOMContentLoaded. ng-view is an asynchronous fetch operation. So, most likely, when the jQuery ready function is triggered to attach your callback upon submit, the ng-view fetch operation of your template hasn't even been completed. So this would fail.
    • It's not best practice to mix jQuery AJAX in your Angular code. The reasons are many for this, but it is mostly because the DOM manipulation paradigm in AngularJS and jQuery are completely different, as can be seen by the point above.
    • What I would advise is for you to convert your AJAX calls to Angular $http requests, within a service. Services are bound to the application and can be used app wide. This will create a clean separation of concerns and avoid the jQuery - Angular race condition

    The searchViaAjax can be moved to be within a controller, like so:

    $scope.searchViaAjax = function() {
      $scope.search.searchResults =  SearchService.search($scope.search.searchText);
      // Where SearchService is a service you define app level, and inject into your controller
    }
    
    // HTML
    <input ng-model="search.searchText">
    <ul>
      <li ng-repeat="searchResult in search.searchResults">{{searchResult.text}}</li>
    </ul>
    

    And so on.

    评论

报告相同问题?

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败