如何将参数传递给$ HTTP在angularjs? [英] How to pass parameters to $http in angularjs?
问题描述
假设我有一个相应的NG-模型FNAME LNAME和两个输入框。如果我打电话的http请求为:
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?
推荐答案
下面是你如何做到这一点:
Here is how you do it:
$http.get("/url/to/resource/", {params:{"param1": val1, "param2": val2}})
.then(function (response) { /* */ })...
美心Shoustin的回答不工作( {方法:GET,网址:'/搜索,jsonData}
不是一个有效的JavaScript文字)和JeyTheva的回答,虽然简单,是很危险的,因为它允许XSS(当你将它们连接起来不安全值不转义)。
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).
这篇关于如何将参数传递给$ HTTP在angularjs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!