Escape % in JSON Struct with Angularjs 发送 http.GET 到服务器
Escape % in JSON Struct with Angularjs for sending http.GET to the server
当我尝试使用 AngularJS 执行查询时遇到问题,因为有像 %
这样的特殊字符。我正在使用 Coldfusion 作为服务器。
查询的URL为:
http://myApp/index.cfm?method=updateRequest&dateRequest=11/12/2017&requestid=29000&jsStruct={%22ID%22:29000,%22PERSONID%22:13541,%22REQUESTDATE%22:%2211/12/2017%22,%22DESCRIPTION%22:%22test%22,%22MAILCONTENT%22:%22essai%2025%20???%%22,%22OPERATOR%22:%22Sebastien%20AMADEI%22,%22REQUESTTHEME%22:{%22ID%22:2,%22OBSOLETE%22:0,%22NAME%22:%22Economy%20and%20finance%22}}
我的工厂:
app.factory('RequestService', function($http){
var factory={};
factory.updateRequest=function(objRequest,id, dateFormat){
return $http.post('http://myApp/requests.cfc?method=updateRequest&dateRequest=' + dateFormat + '&requestid=' + id + '&jsStruct=' + JSON.stringify(objRequest))
};
return factory;
})
文件中我的控制器app.js:
app.controller('ctrlEditRequests', function ($scope, $routeParams, MyTextSearch, RequestService){
RequestService.loadRequestsById($routeParams.requestId).success(function(request){
$scope.submitForm = function(request){
if($scope.RequestForm.$valid){
RequestService.updateRequest(request, $routeParams.personId, dateFormat).success(function(){
window.location="#/view-contacts/" + $scope.request.PERSONID;
}).error(function (data, status, header, config) {
});
}
};
});
这里的结构是:
"{"ID":29000,"PERSONID":13541,"REQUESTDATE":"11/12/2017","DESCRIPTION":"test","MAILCONTENT":"essai 25 ??? 1%1","OPERATOR":"Sebastien AMADEI","REQUESTTHEME":{"ID":2,"OBSOLETE":0,"NAME":"Economy and finance"}}"
我无法执行查询,因为邮件内容字段中有字符%
。我做了 stringify
但还不够。
我不知道如何在发送到服务器以执行查询之前转义特殊字符。
你能帮我解决这个问题吗?
在示例中,"%"
字符被正确编码。这是 "?"
编码不正确的字符。
在URL中,AngularJS不percent encode以下字符:
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
要在 URL 中正确地对搜索参数进行百分比编码,请使用 $http
配置对象的 params
属性:
app.factory('RequestService', function($http){
var factory={};
factory.updateRequest=function(objRequest,id, dateFormat){
var params = {
method: "updateRequest",
dateRequest: dateFormat,
requestid: id,
jsStruct: objRequest
};
var config = { params: params };
return $http.get("http://myApp/requests.cfc", config)
};
return factory;
})
有关详细信息,请参阅
当我尝试使用 AngularJS 执行查询时遇到问题,因为有像 %
这样的特殊字符。我正在使用 Coldfusion 作为服务器。
查询的URL为:
http://myApp/index.cfm?method=updateRequest&dateRequest=11/12/2017&requestid=29000&jsStruct={%22ID%22:29000,%22PERSONID%22:13541,%22REQUESTDATE%22:%2211/12/2017%22,%22DESCRIPTION%22:%22test%22,%22MAILCONTENT%22:%22essai%2025%20???%%22,%22OPERATOR%22:%22Sebastien%20AMADEI%22,%22REQUESTTHEME%22:{%22ID%22:2,%22OBSOLETE%22:0,%22NAME%22:%22Economy%20and%20finance%22}}
我的工厂:
app.factory('RequestService', function($http){
var factory={};
factory.updateRequest=function(objRequest,id, dateFormat){
return $http.post('http://myApp/requests.cfc?method=updateRequest&dateRequest=' + dateFormat + '&requestid=' + id + '&jsStruct=' + JSON.stringify(objRequest))
};
return factory;
})
文件中我的控制器app.js:
app.controller('ctrlEditRequests', function ($scope, $routeParams, MyTextSearch, RequestService){
RequestService.loadRequestsById($routeParams.requestId).success(function(request){
$scope.submitForm = function(request){
if($scope.RequestForm.$valid){
RequestService.updateRequest(request, $routeParams.personId, dateFormat).success(function(){
window.location="#/view-contacts/" + $scope.request.PERSONID;
}).error(function (data, status, header, config) {
});
}
};
});
这里的结构是:
"{"ID":29000,"PERSONID":13541,"REQUESTDATE":"11/12/2017","DESCRIPTION":"test","MAILCONTENT":"essai 25 ??? 1%1","OPERATOR":"Sebastien AMADEI","REQUESTTHEME":{"ID":2,"OBSOLETE":0,"NAME":"Economy and finance"}}"
我无法执行查询,因为邮件内容字段中有字符%
。我做了 stringify
但还不够。
我不知道如何在发送到服务器以执行查询之前转义特殊字符。
你能帮我解决这个问题吗?
在示例中,"%"
字符被正确编码。这是 "?"
编码不正确的字符。
在URL中,AngularJS不percent encode以下字符:
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
要在 URL 中正确地对搜索参数进行百分比编码,请使用 $http
配置对象的 params
属性:
app.factory('RequestService', function($http){
var factory={};
factory.updateRequest=function(objRequest,id, dateFormat){
var params = {
method: "updateRequest",
dateRequest: dateFormat,
requestid: id,
jsStruct: objRequest
};
var config = { params: params };
return $http.get("http://myApp/requests.cfc", config)
};
return factory;
})
有关详细信息,请参阅