繁体   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