0

My Django backend urls look like following:

url(r'^exercises/get_exercise/$', views.get_exercise_state_by_user_id),
url(r'^exercises/get_exercise/(?P<exerciseId>\d)/$', views.get_single_exercise_for_user_id),

I want to hit the second url from my angular controller. 'exerciseId' I want to add dynamically.

Following attempt did not work:

var exerciseType = Number($stateParams.exerciseId); //exerciseType values as obtained from $stateParams are 1,2,3,4,5,6,7 

$http.get("https://localhost:8000/api/exercises/get_exercise/?exerciseId=" + exerciseType)
.then(function(response){

--some action--
}

This one also:

var exerciseType = Number($stateParams.exerciseId); //exerciseType values as obtained from $stateParams are 1,2,3,4,5,6,7 

$http.get("https://localhost:8000/api/exercises/get_exercise/?exerciseId=" + exerciseType + "/")
.then(function(response){

--some action--
}

Coding exerciseId manually in the $http.get url as follows works correct, but that's not what I want.

var exerciseType = Number($stateParams.exerciseId); //exerciseType values as obtained from $stateParams are 1,2,3,4,5,6,7 

$http.get("https://localhost:8000/api/exercises/get_exercise/2/")
.then(function(response){

--some action--
}

I should be able to add it dynamically as shown above. How can that trailing slash be appended after 'exerciseType' in the url. Is there any clean way to achieve that?

3
  • can you console.log exerciseType and see what you get? Commented Mar 28, 2016 at 10:12
  • Come on, do some minimal debugging yourself. The URL you construct has bits in it that aren't in the working URL. ?exerciseId= should be obvious. Commented Mar 28, 2016 at 10:14
  • Yes I get either 1 or 2 or 3 or so until number ,7 as I have only 7 exercise types. That number is also appended to get url but then the complete url becomes "localhost:8000/api/exercises/get_exercise/2" - the trailing slash '/' is missing so it can't hit the desired backend url. Commented Mar 28, 2016 at 10:16

1 Answer 1

1

Alright I couldn't figure out a simple thing. The answer is:

var exerciseType = Number($stateParams.exerciseId); //exerciseType values as obtained from $stateParams are 1,2,3,4,5,6,7 

$http.get("https://localhost:8000/api/exercises/get_exercise/" + exerciseType + "/")
.then(function(response){

--some action--

}

This hits the correct url.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.