programing

파라미터를 angularjs 단위로 $http로 전달하려면 어떻게 해야 합니까?

fastcode 2023. 4. 5. 22:21
반응형

파라미터를 angularjs 단위로 $http로 전달하려면 어떻게 해야 합니까?

fname과 lname으로 대응하는ng-model을 가진 입력 상자가 2개 있다고 가정합니다.http 요청을 호출하는 경우:

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

는, 다음의 URL 에 콜 합니다.

/search?fname=fname&lname=lname

백엔드(python)에서 발생하는 오류는 다음과 같습니다.

cannot concatenate str and nontype objects.

이들 파라미터는 문자열로 전송되지 않습니까?그렇지 않다면 어떻게 대처해야 할까요?

방법은 다음과 같습니다.

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

Angular는 파라미터의 부호화를 처리합니다.

Maxim Shoustin의 답변은 효과가 없습니다.{method:'GET', url:'/search', jsonData}는 유효한 JavaScript 리터럴이 아닙니다.JeyTheva의 답변은 간단하지만 XSS가 가능하기 때문에 위험합니다(안전하지 않은 값은 연결할 때 이스케이프되지 않습니다).

빌드 URL'/search'스트링으로서맘에 들다

"/search?fname="+fname"+"&lname="+lname

사실 안 썼어요.

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

하지만 '패럴'은 확실히JSON.stringify을 좋아하다POST

var jsonData = JSON.stringify(
    {
        fname: fname,
        lname: lname 
    }
);

그 후:

$http({
  method:'GET',
  url:'/search',
  params: jsonData
});

다음으로 루트 프로바이더로부터의 값을 전달하기 위한 간단한 매트를 나타냅니다.

//Route Provider
$routeProvider.when("/page/:val1/:val2/:val3",{controller:pageCTRL, templateUrl: 'pages.html'});


//Controller
$http.get( 'page.php?val1='+$routeParams.val1 +'&val2='+$routeParams.val2 +'&val3='+$routeParams.val3 , { cache: true})
        .then(function(res){
            //....
        })

입력 데이터를 사용하여 입력 필드의 값을 바인딩하기 위해 ng-model을 사용하여 HTML 파일의 파라미터로 전달할 수 있습니다.

<input type="text" placeholder="Enter your Email" ng-model="email" required>

<input type="text" placeholder="Enter your password " ng-model="password" required> 

그리고 js 파일 w에서는 $120을 사용하여 다음 데이터에 액세스합니다.

$scope.email="";
$scope.password="";

컨트롤러의 기능은 다음과 같습니다.

 var app = angular.module('myApp', []);

    app.controller('assignController', function($scope, $http) {
      $scope.email="";
      $scope.password="";

      $http({
        method: "POST",
        url: "http://localhost:3000/users/sign_in",
        params: {email: $scope.email, password: $scope.password}

      }).then(function mySuccess(response) {
          // a string, or an object, carrying the response from the server.
          $scope.myRes = response.data;
          $scope.statuscode = response.status;

        }, function myError(response) {
          $scope.myRes = response.statusText;
      });
    });

언급URL : https://stackoverflow.com/questions/18910054/how-to-pass-parameters-to-http-in-angularjs

반응형