【问题标题】:Grab a users URL with $Location and place in SQL database?使用 $Location 获取用户 URL 并放置在 SQL 数据库中?
【发布时间】:2015-09-25 09:48:57
【问题描述】:

所以我有一个最终用户,他们正在全局 div 上提交一个带有打开弹出窗口的按钮的表单。

长话短说,他们填写此表单并使用 Angular RESTing 方法提交 HTTP 方法。

我试图弄清楚如何从用户访问弹出窗口的位置获取当前 URL,或从其提交。

我想将该 URL 放入 SQL 数据库中,以了解用户在哪里发现错误或从哪里单击此按钮(弹出窗口的目的是获取信息和接收反馈)

解决这个问题的正确方法是什么?

$windows.location? 
$Location? 
$routeparams? 

我真的需要一些帮助。我将在下面发布我的客户端角度,接受这些请求并将其放入 SQL 数据库的后端由我的 C# 控制器完成。

(function() {
'use strict';

var commonModule = angular.module('ambassadors.commonModule');

commonModule.service('commonService', ['$http', '$q', function($http, $q) {

    this.getSettingsView = function() {

        var deferred = $q.defer();

        $http.get('/api/settings')
            .success(function(response) {
                deferred.resolve(response);
            })
            .error(function() {
                deferred.reject();
            });

        return deferred.promise;
    };

    this.getUserClaims = function() {
        var deferred = $q.defer();

        $http.get('/api/profile')
            .success(function(response) {
                deferred.resolve(response);
            })
            .error(function(response) {
                deferred.reject(response);
            });

        return deferred.promise;
    };

    this.submitFeedback = function(feedback, file) {
        var deferred = $q.defer();

        if (file != null) {
            var that = this;
            this.uploadFile(file).then(
                // Success Handler 
                function(result) {
                    // update the scec icon in the controller 
                    feedback.FeedbackUpload = result[0].location;
                    that.postFeedback(feedback, deferred);
                },
                // Failure Handler 
                function() {
                    $scope.messageInfo = "Error uploading feedback file";
                    return deferred.reject;
                });
        } else
            this.postFeedback(feedback, deferred);

        return deferred.promise;
    };

    this.postFeedback = function(feedback, deferred) {

        $http.post('/api/feedback', feedback)
            .success(function() {
                deferred.resolve();
            })
            .error(function() {
                deferred.reject();
            });
    }


    this.uploadFile = function(file) {

        // As per stackoverflow.com/questions/… 
        // uncorkedstudios.com/blog/… 

        var feedbackFileFormData = new FormData();
        feedbackFileFormData.append("file", file);

        var deferred = $q.defer();

        $http.post('/api/feedbackfile', feedbackFileFormData, {
                withCredentials: true,
                headers: {
                    'Content-Type': undefined
                },
                transformRequest: angular.identity
            })
            .success(function(response) {
                deferred.resolve(response);
            })
            .error(function() {
                deferred.reject();
            });

        return deferred.promise;
    };
 }]);
})();

【问题讨论】:

    标签: javascript c# sql angularjs


    【解决方案1】:

    在你的控制器中注入$location服务然后使用

    $location 服务解析浏览器地址栏中的 URL(基于 在 window.location 上)并使 URL 可用于您的 应用。地址栏中 URL 的更改会反映到 $location 服务和对 $location 的更改会反映到 浏览器地址栏。

    var absUrl = $location.absUrl();
    // => "http://example.com/#/some/path?foo=bar&baz=xoxo"
    

    【讨论】:

    • 只是为了弄清楚,我的角度到底在哪里?我的 commonModule.service('commonService', [ 对吗?
    • 是的,你可以在任何地方注入它。只需在servicefactorycontroller 中。快乐的角:-)
    • 点赞commonModule.service('commonService', ['$http', '$q', '$location', function($http, $q, $location) {
    • 非常感谢 Vineet 的帮助,所以我将 $location 信息添加到了我的 commonModule 中。我如何才能将这些信息放入我的数据库中?
    • 在您的AJAX 中传递absUrl 变量值,调用AKA $http.post(),然后将您的值保存在您的数据库中。
    猜你喜欢
    • 2019-02-10
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多