【问题标题】:AngularJs 1.3 $resource is ignoring some charactersAngularJs 1.3 $resource 忽略了一些字符
【发布时间】:2016-04-20 13:10:30
【问题描述】:

我正在维护一个使用旧版本 AngularJs 的项目。我从一开始就没有工作,所以我不能改变太多。

问题是:当我使用 $resource 访问 PHP 脚本时,它忽略了一些字符(如“#”或“\”),所以我认为这是一些 urlenconding 问题(不确定)。请参阅下面的代码。

报告/report.html

<table class="table table-condensed table-bordered table-hover">
        <thead>
            <tr>
                <th>Code</th>
                <th>Date</th>
                <th>Company</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        <tr data-ng-repeat="info in searchResult as results">
            <td>{{info.code}}</td>
            <td>{{info.date}}</td>
            <td>{{info.company_name}}</td>
            <td class="col-sm-2">
            <button data-ng-click="cancel(info.code,info.password)" data-ng-show="!info.canceled" class="form-control btn-danger">Cancel</button>
            </td>
        </tr>
</tbody>

报告/controllers.js

define(['reports/module'], function (app) {
'use strict';

    app.controller('reports', function ($scope, $http, $timeout, cancelService,loading, modal) {
        $scope.cancel = function(code, password) {
            $scope.confirm.code = code;
            $scope.confirm.password = password;
            cancelService.query($scope.confirm, function(return) {
                if(return.status) {
                    alert(return.msg);
                    $scope.searchResult = [];
                }
            });
        };
    });
});

报告/服务.js

define(['reports/module', 'ngResource'], function (app) {
    'use strict';

    app.factory('cancelService', function($resource){
        return $resource('api/reports/cancel-online/reservationno/:code/password/:password',{},{'query' : {'isArray': false}});
    });
});

PHP 脚本 (api/reports/ReportsAPI.class.php)

class ReportsAPI extends Controller {

    //This is a internal method that expects a GET Request using a defined URL
    $restfull = new Restful();
    $restfull->server('GET','api/reports/cancel-online/reservationno/:code/password/:password', function($code, $pass) {
        //do stuff
        //I'm facing the problem here
    }

}

例如,当我使用密码“123#123”调试我的 PHP 时,var $pass 的值是“123”并破坏了我的代码。在调用服务之前执行console.log($scope.confirm.password),我得到了正确的结果,所以看起来是 URLenconding 的问题。

我尝试了在 SO (1,2) 中找到的其他一些方法,但没有成功,我还发现此 issue in GitHub 建议更改 angular 文件,但我没有尝试,因为我没有'不知道它会影响哪些其他软件和平

我该如何解决这个问题?

注意:“密码”只是有问题的变量名,不是真正的密码,所以在javascript中保持打开或通过GET发送没有问题。

以下是版本:

  • AngularJS v1.3.0
  • PHP 5.6
  • 在 Firefox 和 Chrome 中测试

【问题讨论】:

  • 我建议您不要在 url 中传递密码,而是使用 post 方法。您可以使用encodeURIComponent() 对密码进行编码,即$scope.confirm.password = encodeURIComponent(password);
  • 简单,就像一个魅力。非常感谢你。将其发布为答案,我会接受。

标签: javascript php angularjs encoding get


【解决方案1】:

需要对变量password中的特殊字符进行编码,可以使用encodeURIComponent()实现

encodeURIComponent() 方法通过将某些字符的每个实例替换为表示字符的 UTF-8 编码的一个、两个、三个或四个转义序列(将只有四个转义由两个“代理”字符组成的字符序列)。

$scope.confirm.password = encodeURIComponent(password);

【讨论】:

    最近更新 更多