簡體   English   中英

如果發生錯誤,如何更改http發布請求的網址

[英]How can i change the url of a http post request if there is an error

這是處理我的表格的功能

            $scope.processForm = function () {

                var url = 'http://localhost:8080/tickets/'

                $http({
                    method: 'POST',
                    headers: {'Content-Type': 'application/json; charset=UTF-8'},
                    url: url,
                    data: JSON.stringify($scope.formData)
                }).then(function successCallback(response) {
                    //log
                    console.log("ticket purchased");

                }, function errorCallback(response) {
                    var requestID = JSON.stringify(response.data.requestID);
                    console.log("purchase failed");
         });

我想做的是,如果有錯誤,則將requestID附加到URL的末尾。

如果有錯誤,則網址應在再次提交后更改以下內容:

 var url = 'http://localhost:8080/tickets/'+ requestID

您是否希望將requestID附加到您要提交數據的URL的末尾,對嗎?

一種選擇是將URL或requestID存儲在$ scope上。

$scope.url = 'http://localhost:8080/tickets/';

$scope.processForm = function () {

            $http({
                method: 'POST',
                headers: {'Content-Type': 'application/json; charset=UTF-8'},
                url: $scope.url,
                data: JSON.stringify($scope.formData)
            }).then(function successCallback(response) {
                //log
                console.log("ticket purchased");

            }, function errorCallback(response) {
                var requestID = JSON.stringify(response.data.requestID);
                $scope.url = 'http://localhost:8080/tickets/' + requestID;
                console.log("purchase failed");
     });

我想出了最終要實現的目標的方法。 我將網址和requestID保存在$ scope上。

if ($scope.requestID == null) {
    $scope.url = 'http://localhost:8080/tickets/';
} 
else if ($scope.requestID !== null && $scope.firstTransaction == null) {

    $scope.firstRequest = $scope.requestID;

    console.log("first transaction id = " + $scope.requestID)

    $scope.url = 'http://localhost:8080/tickets/' + $scope.firstRequest;

}

$scope.processForm = function() {

        $http({
            method: 'POST',
            headers: {
                'Content-Type': 'application/json; charset=UTF-8'
            },
            url: $scope.url,
            data: JSON.stringify($scope.formData)
        }).then(function successCallback(response) {
            //log
            console.log("ticket purchased");

        }, function errorCallback(response) {
            var requestID = JSON.stringify(response.data.requestID);
            $scope.url = 'http://localhost:8080/tickets/' + requestID;
            console.log("purchase failed");
        });

暫無
暫無

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

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