简体   繁体   English

Angular http返回电话间隙错误,但在浏览器中成功

[英]Angular http returning error in Phone gap but success in browser

I am making an HTTP request in Angular JS. 我在Angular JS中发出HTTP请求。

This works fine in the browser and returns the data successfully . 这在浏览器中可以正常工作,并成功返回数据。 However, when I test on a device for Phonegap usage, it returns an error . 但是,当我在设备上测试Phonegap使用率时,它会返回error

The data for the request is passed to a factory from a controller , below is an example of the code I am using to make the request. 请求的数据从控制器传递到工厂 ,下面是我用来发出请求的代码示例。

HTML HTML

<ul>
    <li ng-repeat="story in stories" ng-click="setUser(story.name)">
        <img ng-src="{{ story.img }}" width="120" height="108" />
            <div>
                <h2>{{ story.name }}'s Story</h2>
                <p>{{ story.title }}</p>
            </div>
    </li>
</ul>

This HTML calls the function setUser(story.name) on ng-click , which passes in the current name of the user in the ng-repeat. 该HTML在ng-click上调用函数setUser(story.name) ,该函数在ng-repeat中传递用户的当前名称。

CONTROLLER CONTROLLER

$scope.setUser = function (story) {
    $scope.chosenStory = story;
    dataFactory.setUser($scope.chosenStory); // HTTP call
}

The controller above gets the value from the HTML's ng-click="setUser(story.name)" and sends it to a factory to make the HTTP request. 上面的控制器从HTML的ng-click="setUser(story.name)"获取值,并将其发送到factory以发出HTTP请求。

FACTORY

setUser : function (val) {
    window.alert(val);
    var username = val;
    window.alert(val);
    $http.get('./js/json/scenarios/' + username + '.json')
        .success(function (data) {
            window.localStorage.setItem("users", JSON.stringify(data));
            $location.path('/story');
        })
        .error(function (data, status) {
            window.alert(val);
            window.alert(data);
            window.alert(status);
        });
    },

In Google's console it logs the success content from the HTTP request. 在Google的控制台中,它记录来自HTTP请求的成功内容。 However, when I test on a device for Phonegap, it always returns the error content of the HTTP request. 但是,当我在用于Phonegap的设备上进行测试时,它总是返回HTTP请求的错误内容。 Eg it always returns the window.alert from the error function of the HTTP request. 例如,它总是从HTTP请求的错误函数返回window.alert。

What I am doing wrong / not doing? 我做错了/没有做? Thanks in advance. 提前致谢。

I'm 99.9% sure this is because of cross domain ajax loading. 我有99.9%的确信这是由于跨域Ajax加载引起的。 Try to enable Access-Control-Allow-Origin as suggested here How to enable CORS in AngularJs or try enabling a whitelist for cross domain of your app as suggest here Phonegap-Javascript sending cross-domain ajax request 尝试按照此处的建议启用Access-Control-Allow-Origin 如何在AngularJs中启用CORS或尝试为您的应用程序的跨域启用白名单,如此处建议的Phonegap-Javascript发送跨域ajax请求

Ultimately, if nothing works and you control the service, you could try changing the response headers, but it's likely that one of those will make it for you. 最终,如果什么都不起作用,并且您控制了服务,则可以尝试更改响应标头,但是很可能其中一个会为您找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM