【问题标题】:Post request gives error on submitting forms data to server发布请求在向服务器提交表单数据时出错
【发布时间】:2015-08-28 01:40:43
【问题描述】:

我有一个表单,我通过发布请求将其数据提交给服务器。这是我在控制器中的请求代码:

        $scope.submitFormData = function (obj, html) {

        var posOptions = {timeout: 10000, enableHighAccuracy: false};
        $cordovaGeolocation
            .getCurrentPosition(posOptions)
            .then(function (position) {
                var lat = position.coords.latitude;
                var long = position.coords.longitude;
                var formId = html.Id;
                var url = base_url+"put/putFormFilledRawData.php?userid="+window.localStorage.userid+"&token="+window.localStorage.token+"&formid="+formId+"&lat="+lat+"&long="+long;
                var data = $(obj.target).serialize();
                $http.post(url, data).
                    success(function(data, status, headers, config) {
                        if(data.alert == 'SUCCESS'){
                            var myPopup = $ionicPopup.show({
                                title: 'Confirmation',
                                subTitle: 'Your data has been submitted successfully',
                                scope: $scope,
                                buttons: [
                                    {
                                        text: '<b>Ok</b>',
                                        type: 'button-calm',
                                        onTap: function(e) {
                                            $(this).hide();
                                        }
                                    },
                                ]
                            });
                        }
                    }).
                    error(function(data, status, headers, config) {
                        var myPopup = $ionicPopup.show({
                            title: 'Error',
                            subTitle: 'An error has been occured try again later!',
                            scope: $scope,
                            buttons: [
                                {
                                    text: '<b>Ok</b>',
                                    type: 'button-calm',
                                    onTap: function(e) {
                                        $(this).hide();
                                    }
                                },
                            ]
                        });
                    });

            }, function (err) {
                var myPopup = $ionicPopup.show({
                    title: 'Location Error',
                    subTitle: 'Not able to get the current location!!!',
                    scope: $scope,
                    buttons: [
                        {
                            text: '<b>Ok</b>',
                            type: 'button-calm',
                            onTap: function(e) {
                                $(this).hide();
                            }
                        },
                    ]
                });
            });
    }
})

当我在我的网络浏览器上运行它并提交表单数据时,日期会以 lat 和 long 形式提交,并且我会收到一个带有 SUCCESS 警报的响应。但是,当我构建应用程序并在我的 Android 手机上尝试它时,请求不会提交,而是执行错误分支并在弹出窗口中向我显示一条自定义消息 An error has been occurred try again later! 所以请求没有从移动设备提交。

我正在构建的应用程序使用带有 Ionic 框架和 AngularJS 的 Cordova。

【问题讨论】:

  • 找出错误回调的数据和状态参数包含什么。
  • 是的,作为@JBNizet,尝试显示这些论文data, status, headers, config,然后更新您的帖子;)

标签: angularjs cordova ionic-framework ionic


【解决方案1】:

最好在代码中的某处显示/记录回调中的错误,就像 JB Nizet 在评论中所说的那样。

变化:

title: 'Error',
subTitle: 'An error has been occured try again later!',

收件人:

title: 'HTTP Error: '+status,
subTitle: data,

在您的错误回调中,您可以看到这些变量datastatus 可用。

【讨论】:

  • 是的,我发现有一个参数问题导致流线数据出现缺陷。非常感谢。事情是我不能在浏览器上做其余的调用,因为它不允许跨站点脚本访问。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
相关资源
最近更新 更多