使用输入文本框更改http post请求中的数据

时间:2015-08-20 12:28:11

标签: angularjs xmlhttprequest

我到目前为止的代码是从Web服务获取数据,该服务接受sql查询并返回JSON数据。当我将myID硬编码到数据库中的ID时,我得到了数据。我试图在运行时使这个可更改,所以我试图使用绑定到myID的输入文本框。当我在myID中输入相同的ID时,这会抛出一个错误的意外令牌。有什么想法我如何将文本框中的文本绑定到我的$ http对象中的命令?感谢!!!

    myApp.controller('myController', function ($scope, $http) {

                  var responsePromise = $http({
                      url: "http://mywebservice/sql",
                      method: "POST",
                      data: {
                          Command: ["select * from mytable where id = " + {{myID}}]
                      },
                      withCredentials: true,
                      headers: {
                          'Content-Type': 'application/json; charset=utf-8',
                          'X-Requested-With': 'XMLHttpRequest'
                      }
                  }).
                  success(function (resp, status, headers, config) {
                      $scope.message = resp.Data;
                      $scope.status = status;
                      $scope.success = resp.Success;
                      $scope.respLogs = resp.Logs[0].Message;
                  }).
                  error(function (resp, status, headers, config) {

                  });

myApp DIV

<input  type="text" ng-model="myID" />

更新1:

试过,仍然没有快乐。当我将文本框中的文本更改为数据库中的ID时,我在Javascript控制台中看不到具有以下错误的响应。

换句话说:

Command: ["select * from [es.device_platf.bug] where id = " + $scope.hsdid]

不起作用,但

Command: ["select * from [es.device_platf.bug] where id = '1234567'"] 作品

SyntaxError: Unexpected token ,
    at Object.parse (native)
    at wc (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:15:401)
    at $b (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:82:143)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:83:50
    at m (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:7:322)
    at fd (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:83:32)
    at c (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:84:211)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:120:182
    at n.$get.n.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:134:493)
    at n.$get.n.$digest (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:132:9)

2 个答案:

答案 0 :(得分:1)

{{myId}}更改为$scope.myID

myApp.controller('myController', function($scope, $http) {

         var responsePromise = $http({
             url: "http://mywebservice/sql",
             method: "POST",
             data: {
                 Command: ["select * from mytable where id = " + $scope.myID] // changes here
             },
             withCredentials: true,
             headers: {
                 'Content-Type': 'application/json; charset=utf-8',
                 'X-Requested-With': 'XMLHttpRequest'
             }
         }).
         success(function(resp, status, headers, config) {
             $scope.message = resp.Data;
             $scope.status = status;
             $scope.success = resp.Success;
             $scope.respLogs = resp.Logs[0].Message;
         }).
         error(function(resp, status, headers, config) {

         });

答案 1 :(得分:0)

  1. 您在JavaScript文件中使用了语法$http(req) .success(function (data, status, headers, config) { // this will call when there is no error on `$http` request. }).error(function (response, status, headers, config) { // this will call when there is error on `$http` request. }); ,但该语法在HTML中使用。

  2. 当您使用{{}}时,您的控制器中ng-model="myID"可以var访问。

  3. 所以改变:

    $scope.myID

    为:

    Command: ["select * from mytable where id = " + {{myID}}]