使用输入文本框更改 http post 请求中的数据
Change the Data in a http post request with input text box
到目前为止,我的代码用于从 Web 服务获取数据,该服务接受 sql 查询和 returns JSON 数据。当我将 myID 硬编码为数据库中的 ID 时,我得到了数据。我试图使它在运行时可变,所以我试图使用绑定到 myID 的输入文本框。当我在 myID 中输入相同的 ID 时,这会引发错误 unexpected token 。有什么想法可以将文本框中的文本绑定到 $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) {
});
我的应用程序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)
已将 {{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) {
});
您在 JavaScript 文件中使用了语法 {{}}
,但在 HTML.
中使用了该语法
当您使用 ng-model="myID"
时,您的控制器中有一个 var
可访问为 $scope.myID
。
所以改变:
Command: ["select * from mytable where id = " + {{myID}}]
至:
Command: ["select * from mytable where id = " + $scope.myID]
到目前为止,我的代码用于从 Web 服务获取数据,该服务接受 sql 查询和 returns JSON 数据。当我将 myID 硬编码为数据库中的 ID 时,我得到了数据。我试图使它在运行时可变,所以我试图使用绑定到 myID 的输入文本框。当我在 myID 中输入相同的 ID 时,这会引发错误 unexpected token 。有什么想法可以将文本框中的文本绑定到 $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) {
});
我的应用程序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)
已将 {{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) {
});
您在 JavaScript 文件中使用了语法
{{}}
,但在 HTML. 中使用了该语法
当您使用
ng-model="myID"
时,您的控制器中有一个var
可访问为$scope.myID
。
所以改变:
Command: ["select * from mytable where id = " + {{myID}}]
至:
Command: ["select * from mytable where id = " + $scope.myID]