簡體   English   中英

角度表單提交兩次(ajax獲取請求)

[英]Angular form submitting twice (ajax get request)

我有一個控制器,由於某種原因,它通過AngularJs中的get請求提交了兩次表單。 我在數據庫中看到表單被提交了兩次,並且在控制台網絡選項卡中,它正在記錄兩個提交,但是,第一個提交的OPTIONS為“請求方法”,第二個為GET。 我認為這可能是一個線索。 我有些困惑,因為我沒有將任何“選項”傳遞給get方法,而只是傳遞給我要提交的URL。

HTML:

<div class="row">
    <div ng-controller="groupEditCtrl">
        <form class="span11" name="" novalidate ng-submit="createArtifact()">
            <legend>Create a new group</legend>
            <div class="row">
                <div class="span5">
                    <div class="control-group">
                        <div class="controls">
                            <input name="text" type="text" placeholder="Group Name" required ng-model="artifact.group_name" />
                        </div>
                    </div>
                </div>
                <div class="span5">
                    <p>
                        <small>What your artifact will look like:</small><br />
                        {{artifact.group_name}}
                    </p>
                </div>
            </div>

            <input name="token" type="hidden" required ng-model="window.token" />
            <div class="control-group">
                <div class="controls controls-row">
                    <button type="submit" class="btn" value="Submit" title="Submit">
                        <span>Submit</span>
                    </button>
                </div>
            </div>

        </form>
    </div>
</div>

控制器:

'use strict';

function groupEditCtrl($scope, $http, $routeParams, $cookie) {

    $scope.createArtifact = function(){
        var requestURL = window.base_url + "/Group/CreateGroup?callback=JSON_CALLBACK&token=" + window.token + "&group_name=" + $scope.artifact.group_name;
        $http.get( requestURL ).
            success(function(data, status, headers, config) {
                console.log('You have successfully submitted a Cause/CreateCause');
            }).
            error(function(data,status,headers,config){
                console.log('You have FAILED submitting a Cause/CreateCause');
            });
    }
};

HTTP選項請求正在向服務器詢問允許其與服務器進行通信的方法(除其他事項外),因此這是正常的事情。

“選項”請求不應在您的后端中進行任何更改,但很可能會記錄下來,如您所見。 仔細檢查它是否沒有更改任何內容(如果是,則后端配置可能不正確,您應該問另一個問題!)。

關於OPTIONS然后GET的角度設置/使用沒有問題。

對我來說,標題:{'Content-Type':undefined}工作,而且我看不到它再次調用兩次。

$http.post("index.php", {id : $scope.A},{
    headers: {'Content-Type': undefined}})
    .success(function (response) {
       $scope.OK = response.POLL;
    }
}

原來這是我不知道的正在構建的自定義api的問題。

暫無
暫無

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

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