简体   繁体   中英

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

Here is the function to process my form

            $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");
         });

What I would like to do is append the requestID onto the end of the url if there is an error.

If there is an error then the url should change the the below once they submit again:

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

You are looking to append the requestID on the end of the url that you are submitting data to, correct?

One option would be to store either the URL or the requestID on $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");
     });

I figured out how to achieve what I wanted in the end. I saved the url and the requestID on $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");
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM