如果出现错误,如何更改http post请求的url

时间:2022-02-26 20:15:44

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.

如果出现错误,我想要做的是将requestID附加到url的末尾。

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

如果有错误,那么一旦他们再次提交,网址应该更改以下内容:

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

2 个解决方案

#1


1  

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

您希望将requestID附加到要提交数据的URL的末尾,对吗?

One option would be to store either the URL or the requestID on $scope.

一种选择是在$ scope上存储URL或requestID。

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

#2


0  

I figured out how to achieve what I wanted in the end. I saved the url and the requestID on $scope.

我想出了如何实现我想要的最终目标。我在$ scope上保存了url和requestID。

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

#1


1  

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

您希望将requestID附加到要提交数据的URL的末尾,对吗?

One option would be to store either the URL or the requestID on $scope.

一种选择是在$ scope上存储URL或requestID。

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

#2


0  

I figured out how to achieve what I wanted in the end. I saved the url and the requestID on $scope.

我想出了如何实现我想要的最终目标。我在$ scope上保存了url和requestID。

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