我正在尝试为ODATA源设置自定义查询(Northwind) - 我在获取URL时收到错误400 - 如果我直接转到浏览器中的URL,但是,我得到相应的结果。是否还应该在函数中指定其他内容?
app.directive('search', function () {
return function ($scope, element) {
element.bind("keyup", function (event) {
let val = element.val();
if(val.length > 2) {
$scope.search(val);
}
});
};
});
app.controller('searchEmployees', function($scope, $http) {
$scope.search = function(val) {
console.log(val);
$http.get(BASE_URL+"Employees?$filter=substringof("+val+", FirstName) eq true")
.then(function(response) {
$scope.rawData = response.data;
})
}});
HTML
<div ng-controller="searchEmployees">
<input type="text" search/>
</div>
如果我尝试搜索f.ex&#39; Nan&#39;因为有一个名为Nancy的员工,它可以使用直接网址(http://services.odata.org/Northwind/Northwind.svc/Employees?$ filter = substringof%28%27Nan %27,%20FirstName%29%20eq%20true),但不是我的代码。
答案 0 :(得分:0)
我必须使用encodeURI和反斜杠,以允许&#39;:
query = encodeURI("Products?$filter=contains(ProductName,\'"+val+"\')");
query = BASE_URL+query;