Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
70 views
in Technique[技术] by (71.8m points)

How to pass parameters to $http in angularjs?

Assume that I have two input boxes with corresponding ng-model as fname and lname. If I call http request as :

$http({method:'GET', url:'/search', params:{fname: fname, lname: lname}})

will it call to the url :

/search?fname=fname&lname=lname

The error I am getting at the backend(python) is :

cannot concatenate str and nontype objects.

Are these parameters not sent as strings? If not, how to get around with this?

question from:https://stackoverflow.com/questions/18910054/how-to-pass-parameters-to-http-in-angularjs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is how you do it:

$http.get("/url/to/resource/", {params:{"param1": val1, "param2": val2}})
    .then(function (response) { /* */ })...

Angular takes care of encoding the parameters.

Maxim Shoustin's answer does not work ({method:'GET', url:'/search', jsonData} is not a valid JavaScript literal) and JeyTheva's answer, although simple, is dangerous as it allows XSS (unsafe values are not escaped when you concatenate them).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...