How to pass parameters to $http in angularjs?

前端 未结 4 1481
囚心锁ツ
囚心锁ツ 2020-12-04 23:45

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         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 00:40

    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).

提交回复
热议问题