如何在angularjs中将参数传递给$http? [英] How to pass parameters to $http in angularjs?
问题描述
假设我有两个输入框,对应的 ng-model 为 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}})
它会调用 url 吗:
will it call to the url :
/search?fname=fname&lname=lname
我在后端(python)遇到的错误是:
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?
推荐答案
这是你的做法:
$http.get("/url/to/resource/", {params:{"param1": val1, "param2": val2}})
.then(function (response) { /* */ })...
Maxim Shoustin 的回答不起作用({method:'GET', url:'/search', 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).
这篇关于如何在angularjs中将参数传递给$http?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!