Reputation: 207
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
Upvotes: 0
Views: 1423
Reputation: 207
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");
});
Upvotes: 0
Reputation: 6748
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");
});
Upvotes: 1